java 数组对象的应用

 public class Student  2 {  3 // 成员变量  4 private String name;  5 private int age;  6  7 // 构造方法  8 public Student()  9  { 10 super(); 11  } 12 13 public Student(String name, int age) 14  { 15 super(); 16 this.name = name; 17 this.age = age; 18  } 19 20 // 成员方法 21 // getXxx()/setXxx() 22 public String getName() 23  { 24 return name; 25  } 26 27 public void setName(String name) 28  { 29 this.name = name; 30  } 31 32 public int getAge() 33  { 34 return age; 35  } 36 37 public void setAge(int age) 38 { 39 this.age = age; 40 } 41 42 @Override 43 public String toString() 44 { 45 return "Student [name=" + name + ", age=" + age + "]"; 46 } 47 }
 1 /**  2 把5个学生的信息存储到数组中,并遍历数组,获取得到每一个学生信息。  3 * 学生:Student  4 * 成员变量:name,age  5 * 构造方法:无参,带参  6 * 成员方法:getXxx()/setXxx()  7 * 分析:  8 * A:创建学生类。  9 * B:创建学生数组(对象数组)。 10 * C:创建5个学生对象,并赋值。 11 * D:把C步骤的元素,放到数组中。 12 * E:遍历学生数组。 13 * */ 14 15 public class Practice 16 { 17 public static void main(String[] args) 18  { 19 // 创建学生数组(对象数组)。 20 Student[] students = new Student[5]; 21 // for (int x = 0; x < students.length; x++) 22 // { 23 // System.out.println(students[x]); 24 // } 25 // System.out.println("---------------------"); 26 27 // 创建5个学生对象,并赋值。 28 Student s1 = new Student("小明", 27); 29 Student s2 = new Student("小红", 30); 30 Student s3 = new Student("小强", 30); 31 Student s4 = new Student("旺财", 12); 32 Student s5 = new Student("张三", 35); 33 34 // 将对象放到数组中。 35 students[0] = s1; 36 students[1] = s2; 37 students[2] = s3; 38 students[3] = s4; 39 students[4] = s5; 40 41 // 遍历 42 for (int x = 0; x < students.length; x++) 43 { 44 //System.out.println(students[x]); 45 Student s = students[x]; 46 System.out.println(s.getName()+"---"+s.getAge()); 47 } 48 } 49 }

15.02 对象数组的内存图解

15.03 集合的由来及与数组的区别

集合类的由来:面向对象语言对事物的体现都是以对象的形式,所以为了方便对多个对象的操作,Java就提供了集合类。

数组和集合类同的区别:

数组可以存储同一种类型的基本数据也可以存储同一种类型的对象,但长度是固定的

集合只可以存储不同类型的对象,长度是可变的

集合类的特点:集合只用于存储对象,集合长度是可变的,集合可以存储不同类型的对象。

15.04 集合的继承体系图解

集合容器因为内部的数据结构不同,有多种具体容器,根据共性内容不断的向上抽取,就形成了集合框架。

框架的顶层Collection接口

15.05 Collection集合的功能概述

Collection 层次结构中的根接口。Collection 表示一组对象,这些对象也称为 collection 的元素。一些 collection 允许有重复的元素,而另一些则不允许。一些 collection 是有序的,而另一些则是无序的。JDK 不提供此接口的任何直接实现:它提供更具体的子接口(如 Set 和 List)实现。此接口通常用来传递 collection,并在需要最大普遍性的地方操作这些 collection。

15.06 Collection集合的基本功能测试

成员方法:

1.  boolean add(Ee):确保此 collection 包含指定的元素(可选操作)。

2.  boolean remove(Objecto):从此 collection 中移除指定元素的单个实例,如果存在的话(可选操作)。

3.  void clear():移除此 collection 中的所有元素(可选操作)。

4.  boolean contains(Objecto):如果此 collection 包含指定的元素,则返回 true。

5.  boolean isEmpty():如果此 collection 不包含元素,则返回 true。

6.  int size():返回此 collection 中的元素数。

例:

 1 // 创建集合对象  2 // Collection c = new Collection(); //错误,因为接口不能实例化  3 Collection c = new ArrayList();  4 c.add("hello");  5 c.add("world");  6 c.add("java");  7 // c.clear();//移除所有元素  8 // System.out.println("remove:" + c.remove("hello"));//移除一个元素  9 // System.out.println("remove:" + c.remove("javaee")); 10 // 判断集合中是否包含指定的元素 11 System.out.println("contains:"+c.contains("hello"));//contains:true 12 System.out.println("contains:"+c.contains("android"));//contains:false 13 //判断集合是否为空 14 System.out.println("isEmpty:"+c.isEmpty());//isEmpty:false 15 //元素的个数 16 System.out.println("size:"+c.size());//size:3 17 System.out.println("c:" + c);//c:[hello, world, java]

15.07 Collection集合的高级功能测试

成员方法:

1.  boolean addAll(Collection<? extends E> c):

将指定 collection 中的所有元素都添加到此 collection 中(可选操作)。

2.  boolean removeAll(Collection<?> c):

移除此 collection 中那些也包含在指定 collection 中的所有元素(可选操作)。

3.  boolean containsAll(Collection<?> c):

如果此 collection 包含指定 collection 中的所有元素,则返回 true。

4.  boolean retainAll(Collection<?> c):

仅保留此 collection 中那些也包含在指定 collection 的元素(可选操作)。换句话说,移除此 collection 中未包含在指定 collection 中的所有元素。

例:

c1.addAll(c2);//将c2集合中的所有元素添加到c1集合中,c1变c2不变 c1.removeAll(c2);//将c1集合中与c2集合相同的所有元素删除,只要有一个相同的就返回true c1.containsAll(c2);//判断c1集合中的元素是否包含c2中的全部元素,全部包含则返回true c1.retainAll(c2);//将c1集合中与c2集合相同的元素保留,删除其他元素,返回值表示c1集合是否发生变化,发生变化返回true,没有变化返回false

15.08 集合的遍历之集合转数组遍历

Object[] toArray():返回包含此 collection 中所有元素的数组。

例:

 1 public class Practice  2 {  3 public static void main(String[] args)  4  {  5 // 创建集合  6 Collection c = new ArrayList();  7 c.add("hello");  8 c.add("world");  9 c.add("java"); 10 11 Object[] objs = c.toArray(); 12 for (int i = 0; i < objs.length; i++) 13  { 14 //向下转为String类型 15 String s = (String)objs[i]; 16 System.out.println(s+":"+s.length()); 17  } 18  } 19 }

运行结果:

hello:5
world:5 java:4

15.09 Collection存储自定义对象并遍历案例(使用数组)

例:

 1 public class Practice  2 {  3 public static void main(String[] args)  4  {  5 // 创建集合  6 Collection c = new ArrayList();  7 //创建学生对象并添加到集合  8 c.add(new Student("小明",23));  9 c.add(new Student("小红",32)); 10 c.add(new Student("小强",14)); 11 c.add(new Student("旺财",8)); 12 c.add(new Student("张三",16)); 13 14 Object[] objs = c.toArray(); 15 for (int i = 0; i < objs.length; i++) 16  { 17 Student s = (Student)objs[i]; 18 System.out.println(s.getName()+":"+s.getAge()); 19  } 20  } 21 }

运行结果:

小明:23
小红:32
小强:14 旺财:8 张三:16

15.10 集合的遍历之迭代器遍历

Iterator<E> iterator():返回在此 collection 的元素上进行迭代的迭代器。

例:

 1 // 创建集合  2 Collection c = new ArrayList();  3 //创建元素并添加到集合  4 c.add("hello");  5 c.add("world");  6 c.add("java");  7 //获取迭代器,实际返回的是子类对象,多态  8 Iterator it = c.iterator();  9 while(it.hasNext()) 10 { 11  System.out.println(it.next()); 12 }

15.11 Collection存储自定义对象并遍历案例(使用迭代器)

 1 public class Practice  2 {  3 public static void main(String[] args)  4  {  5 // 创建集合  6 Collection c = new ArrayList();  7 //创建学生对象并添加到集合  8 c.add(new Student("小明",23));  9 c.add(new Student("小红",32)); 10 c.add(new Student("小强",14)); 11 c.add(new Student("旺财",8)); 12 c.add(new Student("张三",16)); 13 14 Iterator it = c.iterator(); 15 while(it.hasNext()) 16  { 17 Student s = (Student)it.next(); 18 System.out.println(s.getName()+":"+s.getAge()); 19  } 20  } 21 }

15.12 迭代器使用的问题探讨

1.使用迭代器获取元素的两种方式:

方式1:

Iterator it = c.iterator();
while(it.hasNext()) { Student s = (Student)it.next(); System.out.println(s.getName()+":"+s.getAge()); }

方式2:

for(Iterator it = c.iterator();it.hasNext();)
{
   Student s = (Student)it.next(); System.out.println(s.getName()+":"+s.getAge()); }

使用方式2的好处:it在for循环结束后就变成垃圾,效率较高

2.不要多次使用it.next()方法

例:

Iterator it = c.iterator();
while(it.hasNext())
{
   System.out.println(((Student)it.next()).getName()); System.out.println(((Student)it.next()).getAge()); }

上面的代码表示获取的是第1个学生的姓名,第2个学生的年龄,以此类推,如果集合中的元素是奇数个,则会报NoSuchElementException错误

15.13 集合的使用步骤图解

集合的使用步骤:

1.  创建集合对象

2.  创建元素对象

3.  将元素添加到集合

4.  遍历集合

4.1 通过集合对象获取迭代器对象

4.2 通过迭代器对象的hasNext()方法判断是否有元素

4.3 通过迭代器对象的Next()方法获取元素并移动到下一个位置

15.14 迭代器的原理及源码解析

原理:

假设迭代器定义的是一个类,那么就可以创建该类的对象,调用该类的方法来实现集合的遍历,但是Java中提供了很多的集合类,而这些集合类的数据结构是不同的,所以存储的方式和遍历的方式也应该是不同的,进而它们的遍历方式也应该是不一样的。最终就没有定义迭代器类

而无论使用哪种集合都应该具备获取元素的操作,并且最好再辅助与判断功能,也就是说判断功能和获取功能应该是一个集合遍历所具备的,而每种集合的方式又不太一样,所以将这两个功能给提取出来,并不提供具体实现,这种方式就是接口,而真正具体的实现类在具体的子类中以内部类的方式体现的

源码:

public interface Iterator { boolean hasNext(); Object next(); } public interface Iterable { Iterator iterator(); } public interface Collection extends Iterable { Iterator iterator(); } public interface List extends Collection { Iterator iterator(); } public class ArrayList implements List { public Iterator iterator() { return new Itr(); } //内部类 private class Itr implements Iterator { public boolean hasNext() {} public Object next(){} } } Collection c = new ArrayList(); c.add("hello"); c.add("world"); c.add("java"); Iterator it = c.iterator(); //new Itr(); while(it.hasNext()) { String s = (String)it.next(); System.out.println(s); }

15.15 Collection存储字符串并遍历

 1 import java.util.ArrayList;
 2 import java.util.Collection;  3 import java.util.Iterator;  4  5 public class Practice  6 {  7  8 public static void main(String[] args)  9  { 10 11 // 创建集合 12 Collection c = new ArrayList(); 13 14 //添加字符串 15 c.add("hello"); 16 c.add("你好"); 17 c.add("world"); 18 c.add("java"); 19 c.add("旺财"); 20 //通过集合对象获取迭代器对象 21 22 Iterator it = c.iterator(); 23 while(it.hasNext()) 24 25  { 26 String s = (String)it.next(); 27 28  System.out.println(s); 29  } 30 31  } 32 33 }

15.16 Collection存储学生对象并遍历

 1 import java.util.ArrayList;
 2 import java.util.Collection;  3 import java.util.Iterator;  4  5 public class Practice  6 {  7 public static void main(String[] args)  8  {  9 // 创建集合 10 Collection c = new ArrayList(); 11 //创建学生对象并添加到集合 12 c.add(new Student("小明",23)); 13 c.add(new Student("小红",32)); 14 c.add(new Student("小强",14)); 15 c.add(new Student("旺财",8)); 16 c.add(new Student("张三",16)); 17 18 Iterator it = c.iterator(); 19 while(it.hasNext()) 20  { 21 Student s = (Student)it.next(); 22 System.out.println(s.getName()+":"+s.getAge()); 23  } 24  } 25 }

15.17 List存储字符串并遍历

 1 public class Practice  2 {  3 public static void main(String[] args)  4  {  5 // 创建集合  6 List list = new ArrayList();  7 list.add("hello");  8 list.add("world");  9 list.add("java"); 10 11 Iterator it = list.iterator(); 12 while(it.hasNext()) 13  { 14 String s = (String)it.next(); 15  System.out.println(s); 16  } 17  } 18 }

15.18 List集合的特点

List接口概述:有序的(存取顺序一致)collection(也称为序列)。此接口的用户可以对列表中每个元素的插入位置进行精确地控制。用户可以根据元素的整数索引(在列表中的位置)访问元素,并搜索列表中的元素。

特点:与 set 不同,列表通常允许重复的元素。

15.19 List存储学生对象并遍历

 1 public class Practice  2 {  3 public static void main(String[] args)  4  {  5 // 创建集合  6 List list = new ArrayList();  7 //创建学生对象并添加到集合  8 list.add(new Student("小明",23));  9 list.add(new Student("小红",32)); 10 list.add(new Student("小强",14)); 11 list.add(new Student("旺财",8)); 12 list.add(new Student("张三",16)); 13 14 Iterator it = list.iterator(); 15 while(it.hasNext()) 16  { 17 Student s = (Student)it.next(); 18 System.out.println(s.getName()+":"+s.getAge()); 19  } 20  } 21 }

15.20 List集合的特有功能概述和测试

1.  void add(int index,Eelement):

在列表的指定位置插入指定元素(可选操作)。

2.  Eremove(int index):

移除列表中指定位置的元素(可选操作)。

3.  Eget(int index):

返回列表中指定位置的元素。

4.  Eset(int index, Eelement):

用指定元素替换列表中指定位置的元素(可选操作)。

例:

list.add(2,"javaee");//在2的位置插入javaee,改变集合长度

list.get(2)//返回集合中2位置上的元素,不改变集合长度

list.remove(1)//删除集合中1位置上的元素,返回被删除的元素,改变集合长度

list.set(2, "javaee")//将集合中2位置上的元素替换为javaee,返回被替换的元素,不改变集合长度

15.21 List集合的特有遍历功能

1 for (int i = 0; i < list.size(); i++) 2 3 { 4 5 String s = (String)list.get(i); 6 7  System.out.println(s); 8 9 }

15.22 List存储自定义对象并遍历(使用List特有功能遍历)

 1 public class Practice  2 {  3 public static void main(String[] args)  4  {  5 // 创建集合  6 List list = new ArrayList();  7 //创建学生对象并添加到集合  8 list.add(new Student("小明",23));  9 list.add(new Student("小红",32)); 10 list.add(new Student("小强",14)); 11 list.add(new Student("旺财",8)); 12 list.add(new Student("张三",16)); 13 14 for (int i = 0; i < list.size(); i++) 15  { 16 Student s =(Student)list.get(i); 17 System.out.println(s.getName()+":"+s.getAge()); 18  } 19  } 20 }

15.23 ListIterator的特有功能

ListIterator<E> listIterator():

返回此列表元素的列表迭代器(按适当顺序)。

注意:ListIterator可以实现逆向遍历,但是必须先正向遍历,才能逆向遍历

例:

 1 public class Practice  2 {  3 public static void main(String[] args)  4  {  5 // 创建集合  6 List list = new ArrayList();  7  8 list.add("hello");  9 list.add("world"); 10 list.add("java"); 11 //列表迭代器 12 ListIterator lit = list.listIterator(); 13 //正向遍历 14 while(lit.hasNext()) 15  { 16 String s = (String)lit.next(); 17  System.out.println(s); 18  } 19 System.out.println("-----"); 20 //逆向遍历 21 while(lit.hasPrevious()) 22  { 23 //获取上一个元素 24 String s = (String)lit.previous(); 25  System.out.println(s); 26  } 27 28  } 29 }

运行结果:

hello

world

java

-----

java

world

hello

15.24 并发修改异常的产生原因及解决方案

例:

 1 public class Practice  2 {  3 public static void main(String[] args)  4  {  5 // 创建集合  6 List list = new ArrayList();  7  8 list.add("hello");  9 list.add("world"); 10 list.add("java"); 11 Iterator it = list.iterator(); 12 while(it.hasNext()) 13  { 14 String s = (String)it.next(); 15 if(s.equals("world")) 16 list.add("javaee"); 17 } 18  System.out.println(list); 19  } 20 }

上面的代码会运行错误,发生ConcurrentModificationException异常

错误产生原因:迭代器是依赖于集合存在的,在迭代的过程中使用集合的方法添加元素迭代器是不知道的,所以报错,并发修改异常

解决方案:1.用迭代器迭代元素时使用迭代器修改元素(ListIterator列表迭代器),添加的元素在迭代的元素后面

2.用集合遍历元素,用集合修改元素(for循环),添加的元素在最后

15.25 数据结构之栈和队列

数据结构:数据的组织方式

15.26 数据结构之数组和链表

数组:存储同一种类型的多个元素的容器

链表:由一个链子把多个节点(数据和节点)连起来组成的数据

15.27 List的三个子类的特点

ArrayList:底层数据结构是数组,查询快,增删慢,是不同步的,线程不安全,效率高

Vector:底层数据结构是数组,查询快,增删慢,是同步的,线程安全,效率低

LinkedList:底层数据结构是链表,查询慢,增删快,是不同步的,线程不安全,效率高

猜你喜欢

转载自www.cnblogs.com/riverone/p/11800487.html