建设信用中国网站,淘宝客优惠券网站怎么做,网站建设步骤 文档,做网站电话沧州Java编程问题top100---基础语法系列一一、Java 操作符实质二、将InputStream转换为String使用IOUtils自己写轮子三、将数组转换为List四、如何遍历map对象使用For-Each迭代entries#xff08;方法一#xff09;使用For-Each迭代keys和values#xff08;方法二#xff09;使…
Java编程问题top100---基础语法系列一一、Java 操作符实质二、将InputStream转换为String使用IOUtils自己写轮子三、将数组转换为List四、如何遍历map对象使用For-Each迭代entries方法一使用For-Each迭代keys和values方法二使用Iterator迭代方法三使用泛型不使用泛型迭代keys并搜索values低效的方法四总结五、publicprotectedprivate不加修饰符。有什么区别呢Java编程问题top100---基础语法系列导航记得这个是当时刚入学那会尝试去看的一个github上的优秀内容当时很多东西看得云里雾里不太懂。现在正好有空了再来一次会稍作修改再加上代码实践有问题可以留言。重温的同时希望对你能有所帮助。以下系列将会是对stackoverflow上Java相关、投票数TOP100的问答(精修版)。一、Java 操作符实质
问题我之前以为 i j 等同于 i i j; 但假设有 int i 5; long j 8; 这时 i i j 不能编译但 i j 却可以编译。这说明两者还是有差别的 这是否意味着i j实际是等同于 i (type of i) (i j)呢
// Java 操作符实质
public class test01 {public static void main(String[] args) {int i 5;long j 8;// i i j;// 编译报错需要的类型int提供了longi j; // 这行没有问题i i (int) j; // 这行也没问题}
}回答 对复合赋值表达式E1 op E2来说 (诸如 i j i - j i*j i/j等等)其实是等同于 E1 (T)((E1) op (E2)) 其中E1和E2表示两个操作数T是E1这个元素的类型。 举例来说如下的代码 int i 5;long j 8; i j;等同于i (int)(i j); 也就是(i j)先执行然后(i j)的结果强制类型转换为int类型 新人注意表达式i (int)(i j); 里(i j)这个是带括号的。 它也等价于i i (int) j;
二、将InputStream转换为String
首先项目名字那按右键选Add Framework Support或者选添加框架支持然后选Maven,在pom.xml里写入下面这个 dependenciesdependencygroupIdcommons-io/groupIdartifactIdcommons-io/artifactIdversion2.4/version/dependency/dependencies使用IOUtils
最靠谱的方法用Apache commons IOUtils 文档test02.txt
ABC a b ABc b
你好//将InputStream转换为String
public class test02 {public static void main(String[] args) throws Exception {FileInputStream inputStream new FileInputStream(D:\test02.txt);String encoding UTF-8;StringWriter writer new StringWriter();IOUtils.copy(inputStream, writer, encoding);String theString writer.toString();// String theString IOUtils.toString(inputStream, encoding);// 这个方法其实封装了上面的方法减少了一个参数}
}或者
String theString IOUtils.toString(inputStream, encoding);
//这个方法其实封装了上面的方法减少了一个参数 自己写轮子
如果不想引入Apache库也可以这样做
//将InputStream转换为String
public class test02 {static String convertStreamToString(InputStream is) {Scanner s new Scanner(is).useDelimiter(你好);//读取到你好这个词就会停止输入return s.hasNext() ? s.next() : ;}public static void main(String[] args) throws FileNotFoundException {FileInputStream fileInputStream new FileInputStream(test.txt);System.out.println(convertStreamToString(fileInputStream));}
}三、将数组转换为List
1、最简单的for循环遍历挨个赋值给List。 2、Arrays.asList(array);
// 将数组转换为List
public class test03 {public static void main(String[] args) {Integer[] array {1, 2, 3, 4};ListInteger arrayLists Arrays.asList(array);// 这里System.out.println(arrayLists);}
}但这样做会有坑
生成的list是固定长度的。也就是说如果调用它的add()方法或者remove()方法都会抛异常UnsupportedOperationException。如果修改数组的值list中的对应值也会改变
因为Arrays.asList() 返回的是Arrays里的内部静态类而不是Java.util.ArrayList这个类。 这个java.util.Arrays.ArrayList有set(),get(),contains()方法但是没有任何add() 方法所以它是固定大小的.
建议用这个 Collections.addAll(arrayLists, array);
// 将数组转换为List
public class test03 {public static void main(String[] args) {Integer[] array {1, 2, 3, 4};ListInteger arrayLists new ArrayList();Collections.addAll(arrayLists, array);// 这里}
}四、如何遍历map对象
在Java中有多种遍历HashMap的方法。 让我们回顾一下最常见的方法和它们各自的优缺点。 由于所有的Map都实现了Map接口所以接下来方法适用于所有Map如HaspMapTreeMapLinkedMapHashTableConcurrentHashMAp……
使用For-Each迭代entries方法一
这是最常见的方法并在大多数情况下更可取的。当你在循环中需要使用Map的键和值时就可以使用这个方法
public class test04 {public static void main(String[] args) {MapString, Integer map new HashMap();// map null;map.put(a, 1);testOne(map);}public static void testOne(MapString, Integer map) {if (map ! null !map.isEmpty()) {for (Map.EntryString, Integer entry : map.entrySet()) {System.out.println(key entry.getKey() , value entry.getValue());}}else {System.out.println(map);}}
}注意遍历之前你应该判断是否为空引用。如果遍历的map是null的话For-Each循环会抛出NullPointerException异常。
使用For-Each迭代keys和values方法二
如果你只需要用到map的keys或values时你可以遍历KeySet或者values代替entrySet public static void testTwo(MapString, Integer map) {if (map ! null !map.isEmpty()) {for (String key : map.keySet()) {System.out.println(Key key);}for (Integer value : map.values()) {System.out.println(Value value);}}}这个方法比entrySet迭代具有轻微的性能优势(大约快10%)并且代码更简洁
使用Iterator迭代方法三
使用泛型 public static void testThree(MapString, Integer map) {IteratorMap.EntryString, Integer entries map.entrySet().iterator();while (entries.hasNext()) {Map.EntryString, Integer entry entries.next();System.out.println(Key entry.getKey() , Value entry.getValue());}}不使用泛型
public static void testFour(MapString, Integer map) {if (map ! null !map.isEmpty()) {Iterator entries map.entrySet().iterator();while (entries.hasNext()) {Map.Entry entry (Map.Entry) entries.next();String key (String) entry.getKey();Integer value (Integer) entry.getValue();System.out.println(Key key , Value value);}} else {System.out.println(map);}}你可以使用同样的技术迭代keyset或者values 首先它是遍历java 5 以前版本的map的唯一方法。 第二是可以让你在迭代的时候从map中删除entries的(通过调用iterator.remover())唯一方法。 如果你试图在For-Each迭代的时候删除entries你将会得到unpredictable resultes 异常。 从性能方面看这个方法等价于使用For-Each迭代方法二
迭代keys并搜索values低效的方法四 public static void testFive(MapString, Integer map) {if (map ! null !map.isEmpty()) {for (String key : map.keySet()) {Integer value map.get(key);System.out.println(Key key , Value value);}} else {System.out.println(map);}}比方法一更简洁但是实际更慢更低效通过key得到value值更耗时
总结
如果你只需要使用key或者value使用方法二使用For-Each迭代keys和values如果你使用java 5 以前的版本或者打算在迭代的时候移除entries使用方法三使用Iterator迭代。其他情况请使用方法一For-Each迭代entries方法。避免使用迭代keys并搜索values低效的方法。
五、publicprotectedprivate不加修饰符。有什么区别呢
如下表所示,Y表示能访问(可见性N表示不能访问例如第一行的第3个Y表示类的变量/方法如果是用public修饰它的子类能访问这个变量/方法
修饰符类内部同个包package子类其他范围publicYYYYprotectedYYYN无修饰符YYN or Y(见说明NprivateYNNN
说明 需要特别说明“无修饰符”这个情况子类能否访问父类中无修饰符的变量/方法取决于子类是否在同一个包中。如果子类和父类在同一个包中那么子类可以访问父类中的无修饰符的变量/方法否则不行。
Java编程问题top100—基础语法系列导航
Java编程问题top100—基础语法系列一 Java编程问题top100—基础语法系列二 待更新 Java编程问题top100—基础语法系列三 待更新
如有错误还请多多指教 转载或者引用本文内容请注明来源及原作者橘足轻重