2016/5/29学习记录

1.多态和动态绑定的例子,有助于理解

 1 public class Test {
 2     public static void main(String[] args) {
 3         new A();
 4         new B();
 5     }
 6 }
 7 
 8 class A {
 9     public int i = 7;
10 
11     public A() {
12         setI(20);
13         System.out.println("the number of i in A is " + i);
14     }
15 
16     public void setI(int i) {
17         this.i = 2 * i;
18     }
19 
20 }
21 
22 class B extends A {
23     public B() {
24         System.out.println("the i in b is " + i);
25     }
26 
27     public void setI(int i) {
28         this.i = 3 * i;
29     }
30 }

输出为

the number of i in A is 40
the number of i in A is 60
the i in b is 60
View Code

2.ArrayList类的用法[对象列表/对象数组]

 ArrayList类包含的方法有+ArrayList()/+add()/+add(index,inti)/+clear/+contains/+get(intdex)/+indexOf(object)/+isEmpty()/+remove()/+set(index,inti)

 创建方式ArrayList<String> list = new ArrayList<> ();

 对ArrayList类可以使用遍历,这个和数组是一样的;

 从数组中创建对象列表{

  String[] array = new String[5];

  ArrayList<String> list = new ArrayList<>(Arrays.asList(array));  //这里使用了Arrays类的asList方法进行复制。

}  

 反过来,从对象列表创建数组{

 ArrayList<String> list = new ArrayList<>();

 String[] b = new String[list.size()];

 list.toArray(b); 

} 

 若数组里包含的是数值型的对象,可以进行排序如下

 Double [] array = {1.2,1,3,5.3,0.3};

 Arrays.sort(array);

 同样的对于对象列表 

 ArrayList<String> list = new ArrayList<>(Arrays.asList(array));

 java.util.Collections.sort(list);

 其中Collections有很多其他用法很方便。例如Collections.max(list)/Collections.min(list)/Collections.shuffle(list) //打乱 

  

 

原文地址:https://www.cnblogs.com/laigaoxiaode/p/5540841.html