java对象数组的概述和使用
- 1 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:底层数据结构是链表,查询慢,增删快,是不同步的,线程不安全,效率高
java对象数组的概述和使用的更多相关文章
- JSon_零基础_007_将JSon格式的"数组"字符串转换为Java对象"数组"
将JSon格式的"数组"字符串转换为Java对象"数组". 应用此技术从一个json对象字符串格式中得到一个java对应的对象. JSONObject是一个“n ...
- java对象数组
问题描述: java 对象数组的使用 问题解决: 数组元素可以是任何类型(只要所有元素具有相同的类型) 数组元素可以是基本数据类型 数组元素也可以是类对象,称这样的数组为对象数组.在这种情况下 ...
- java 对象数组
java 对象数组 from zhaocundang@163.com 先 用类声明数组: 再把类的实例赋给数组: package works; import java.util.Scanner; pu ...
- java对象生命周期概述复习
最近看了下java对象的生命周期做个笔记复习复习,很多不同的原因会使一个java类被初始化,可能造成类初始化的操作: 1) 创建一个java类的实例对象. 2) 调用一个java类中的静态方法. ...
- java 对象数组定义
下面代码实现了定义一个数组对象 public class Student { private String username; private int num; public Student(Stri ...
- JAVA 对象数组,加载图片实例 分类: Java Game 2014-08-14 16:57 80人阅读 评论(0) 收藏
主函数: package com.mywork; import java.awt.Color; import java.awt.Image; import javax.swing.ImageIcon; ...
- json字符串转java对象数组
需要引入json-lib-2.2-jdk15.jar和ezmorph-1.0.6.jar包 String itemStar = request.getParameter("itemStar& ...
- 对象数组、集合、链表(java基础知识十五)
1.对象数组的概述和使用 * 需求:我有5个学生,请把这个5个学生的信息存储到数组中,并遍历数组,获取得到每一个学生信息. Student[] arr = new Student[5]; //存储学生 ...
- 初识Java对象
初始Java对象 本文的概述顺序 1什么是面向对象编程(面向对象编程与 面向过程编程的区别) 2类和对象的的关系 3类的定义 4对象的创建 5对象使用的一些细节 5.1对象在内存中的产生及分布 5.2 ...
随机推荐
- bootstrap上传表单的时候上传的数据默认是0 一定要小心
bootstrap上传表单的时候上传的数据默认是0 一定要小心
- C#winfrom中应用程序只启动一次代码
static class Program { private const int WS_SHOWNORMAL = 1; [DllImport("User32 ...
- [python] 视频008
悬挂else if(hi>2) if(hi>7) printf('aaa') else printf('b') c语言中else会与就近if匹配 三元操作符 small=x if x< ...
- django中文件(头像)上传
一些常用的小功能,记录下来 先说一种上传功能. 模板表单 <form enctype="multipart/form-data" name="form1" ...
- WebApi(二)-重新封装返回结果
先创建要返回的结果类型: /// <summary> /// 返回类型 /// </summary> public class ApiResultModel { private ...
- Java 高效检查一个数组中是否包含某个值
如何检查一个数组(未排序)中是否包含某个特定的值?在Java中,这是一个非常有用并又很常用的操作.同时,在StackOverflow中,有时一个得票非常高的问题.在得票比较高的几个回答中,时间复杂度差 ...
- 下一代hadoop
1,hadoop 2.0 产生背景2,hadoop 2.0 基本构成3,HDFS 2.04 YARN5 MapReduce On YARN6 Hadoop 2.0初体验7 总结 1,hadoop 2. ...
- open files
/* * * Copyright (c) International Business Machines Corp., 2001 * * This program is free software; ...
- 编写优质无错C程序秘诀!《经验谈》
这里我将陆续给大家载出我在以前学习和编写c代码时遇到的问题和解决方法.学习的心得,有些是经过查询一些曾经参加微软microsoft的开发小组的老程序员的书籍和资料后提供给大家! 首先,当发现错误时,要 ...
- 【转】如图,win7登陆界面,键盘失灵,没办法登陆。求解!如何在这个界面打开个鼠标可以点的软键盘
原文网址:http://zhidao.baidu.com/link?url=URPzHJXt9_yhtE-2A89apKsn5Y1B9O2NR_mktkaHSOPbUWb7TKSIYJKj_-lYPn ...