List接口&及其子类ArrayList

System.out.print()方法默认调用了打印对象的toString()方法。

toString()方法一般是要重写的

 

当一个父类有有参构造的时候,一定要有无参构造,因为子类继承的时候,会默认调用父类的无参构造,如果父类没有,那么编译器就会报错。

super指代的是直接父类

凡是继承,除了private 修饰的  和 构造方法,其他的都会有,即使不写,默认都会有。

size()是集合中求元素个数的方法,等同于数组中的length()

for(Object o:c){

syso(o);

}

 

迭代:就像普通for循环一样,i每次迭代数组中的元素,这就叫迭代。

迭代器Iterator  和增强for循环在集合遍历中的区别:

增强for循环在删除集合元素时可能会出错,而迭代器不会。

其余的没有什么太大的区别,只是迭代器是集合常用的遍历方法而已。

 

Math.PI  :   PI封装在Math类中,而且用static修饰,所以可以直接打点调用。

 

集合中存储基本数据类型,存储的都是包装类。

 

迭代器是接口,但是之所以可以创建对象,是利用它的子类来创建对象。

Iterator it =c.iterator();  it 是Iterator子类的对象。

 

在编译器中  将代码所有的类都写在一页,和分页写,只有访问修饰符不同的区别,其余没有区别。

利用编译期提供的重写equals方法时,可以选择要判断哪个属性,不一定都要判断,比如只判断name  就可以只打钩name,而不判断其他的属性。

迭代器是结合特有的遍历方法。

 

接口就是一个约定,规范,协议。

 

迭代器中的 hasNext()方法和 next()方法要配对使用,负责可能出现空指针异常。  迭代器在集合中非常重要,要重点掌握。

 

List集合:有序可重复的集合

有序:插入顺序与存储顺序相同。

例如 :

a.add(1);

a.add(9);

a.add(7);

打印出来的为 1,9,7  而不为  1,7,9  因为是插入顺序与存储顺序相同。

 

get()方法要重点掌握

 

DT时代:数据时代

 

泛型:将运行期异常转变为编译期异常

泛型:参数化类型。

<>中放入想存储的类型。

 

集合中使用了泛型的话,就只能 存储一种对象了,那样的话,数据和集合的区别就是:集合封装了方法,使用起来比较方便,数组没有封装方法,使用起来不方便,比如,要删除数组中的第二个元素,要写一个for循环,而要删除集合中的第二个元素,只要调用方法就好了。

在JAVA1.5版本以后, int类型与Integer类型的转换,是JAVA后台自动运行的,这叫自动装箱和自动拆箱。

 

集合中的常用方法:

remove (Object  o)  删除o对象

    注意:默认任然调用equals方法来判断是否包含,对于自定义来说,必须得重写哈希方法和equals方法才可以使用。

代码示例:

 1 public class  集合中常用方法 {
 2 public static void main(String[] args) {
 3 Collection c=new ArrayList<>();
 4 // System.out.println(c);
 5 //isEmpty  判断集合是否为空元素。
 6 // System.out.println(c.isEmpty());
 7 /*c.add(1);
 8 c.add(2);
 9 c.add(3);
10 c.add(4);
11 c.add(5);
12 System.out.println(c);
13 System.out.println(c.isEmpty());*/
14 Collection c1=new ArrayList<>();
15 c1.add(new P1("zs", 19));
16 c1.add(new P1("zs1", 19));
17 c1.add(new P1("zs2", 19));
18 c1.remove(new P1("zs", 19));
19 System.out.println(c1);
20 }
21 }
22 class P1{
23 String name;
24 int age;
25 public P1() {
26 super();
27 // TODO Auto-generated constructor stub
28 }
29 public P1(String name, int age) {
30 super();
31 this.name = name;
32 this.age = age;
33 }
34 @Override
35 public String toString() {
36 return "P1 [name=" + name + ", age=" + age + "]";
37 1
38 return "P1 [name=" + name + ", age=" + age + "]";
39 }
40 @Override
41 public int hashCode() {
42 final int prime = 31;
43 int result = 1;
44 result = prime * result + age;
45 result = prime * result + ((name == null) ? 0 : name.hashCode());
46 return result;
47 }
48 @Override
49 public boolean equals(Object obj) {
50 if (this == obj)
51 return true;
52 if (obj == null)
53 return false;
54 if (getClass() != obj.getClass())
55 return false;
56 P1 other = (P1) obj;
57 if (age != other.age)
58 return false;
59 if (name == null) {
60 if (other.name != null)
61 return false;
62 } else if (!name.equals(other.name))
63 return false;
64 return true;
65 }
66 }

 

 

  集合的遍历:

  1.增强for循环

  2.迭代器: Ieterator

        hashNext()  判断是否有元素

        next()  返回该位置的元素

代码示例:

 1 public class  集合的遍历 {
 2 public static void main(String[] args) {
 3 Collection c=new ArrayList<>();// int a=5;
 4 c.add(1);
 5 c.add(2);
 6 c.add(3);
 7 c.add(4);
 8 // System.out.println(c);
 9 // 遍历集合
10 // 增强 for
11 /*for(Object o:c) {
12 System.out.println(o);
13 }*/
14 //***** 迭代器 : Iterator -- 》集合的 iterator  获取到该接口
15 Iterator it=c.iterator();
16 /*System.out.println(it.hasNext());
17 System.out.println(it.next());
18 System.out.println(it.hasNext());
19 System.out.println(it.next());*/
20 while(it.hasNext()) {
21 Object o=it.next();
22 System.out.println(o);
23 }
24 }
25 }

 

List集合:

List集合中的常用方法:  

  add(int index,E element)  在index索引位置,插入元素

  get(index):通过index返回该索引位置的元素

  set(int  index, E element)  设置index位置的元素为参数

 

代码示例:

 1 public class List 集合接口 {
 2 public static void main(String[] args) {
 3 List list=new ArrayList<>();
 4 list.add(1);
 5 list.add(9);
 6 list.add(7);
 7 // System.out.println(list);
 8 // add(int index, E element) 在 index 索引位置 插入元素。
 9 // list.add(1, 2);
10 // get(index)  通过 index 返回该索引位置的元素
11 // System.out.println(list.get(2));
12 // 逆序遍历 其中,如果为逆序,则指针会上移,所以需要将指针置为长度位置。
13 /*ListIterator it = list.listIterator(list.size());
14 while(it.hasPrevious()) {
15 System.out.println(it.previous());
16 }*/
17 System.out.println(list);
18 //set(int index, E element) 设置 iindex 位置的元素为参数
19 list.set(1, 10);
20 System.out.println(list);
21 }
22 }

 

集合的泛型:

好处:将运行期的异常转换为编译期的异常

   类型可以转化

参数化类型,格式: <集合中存储的数据类型>

注意:尽管集合中可以存储任意类型的对象,但是一般需要加泛型,指定该集合只存储一种数据。

代码演示:

1 // 带有泛型的集合
2 ArrayList<Person> a=new ArrayList<>();
3 a.add(new Person(" 张三 ", 18));
4 a.add(new Person(" 李四 ", 18));
5 a.add(new Person(" 王五 ", 18));
6 a.add(new Person("maliu", 19));

 

集合的遍历:

代码示例:

 1 List 集合的遍历 :
 2 *①  增强 for 循环
 3 *②  迭代器 Iterator 接口
 4 ③ List 特有的迭代器接口 : ListIterator
 5for 循环
 6 代码演示:
 7 // 带泛型的集合的遍历 迭代器也要加泛型。否则,数据类型为 Object
 8 Iterator<Person> it= a.iterator();
 9 while(it.hasNext()) {
10 System.out.println(it.next().name);
11 }

 

 

 

 

 

  

 

posted @ 2019-04-04 19:26  ControllerMe  阅读(579)  评论(0编辑  收藏  举报