Java新手:对象数组以及ArrayList集合类

对象数组
  • 基本类型的数组:存储的元素是基本类型,例如int[] arr ={1,2,3};
  • 对象数组:存储的元素是引用类型。例如Student s = new Student[3];Student是一个自定义的学生类,s[0],s[1],s[2]的元素类型都是Student类,都可以指向一个Student对象。
//自定义的学生类
publicclass Student {
	private String name;
	private int age;	
	//自动生成无参构造方法:代码区域右键 -- Source -- Generate Constructors from Superclass...	
	public Student() {		
	}
	//自动生成带参构造方法:代码区域右键 -- Source -- Generate Constructor using Fields...		
	public Student(String name, int age) {
		this.name = name;
		this.age = age;
	}
	//自动生成getXxx()/setXxx():代码区域右键 -- Source -- Generate Getters and Setters...
	public String getName() {
		returnname;
	}
	publicvoid setName(String name) {
		this.name = name;
	}
	publicint getAge() {
		returnage;
	}
	publicvoid setAge(int age) {
		this.age = age;
	}	
}

测试类,生成学生数组,加入数组元素并且遍历学生数组

publicstaticvoid main(String[] args) {
		//创建学生数组
		Student[] students = new Student[3];
		//创建学生对象
		Student s1 = new Student("小二",40);
		Student s2 = new Student("张三",35);
		Student s3 = new Student("李四",30);
		
		//把学生对象作为元素赋值给学生数组
		students[0] = s1;
		students[1] = s2;
		students[2] = s3;
		
		//遍历学生数组
		for(int x=0; x<students.length; x++) {
			Student s = students[x];
			System.out.println(s);
			System.out.println(s.getName()+"---"+s.getAge());
		}
	}
}

输出结果:

s=com.ArrayListTest.Student@4f9bbd86
小二---40
s=com.ArrayListTest.Student@79b7d13e
张三---35
s=com.ArrayListTest.Student@130a7be0
李四---30

注意: 类的class文件、类中的成员变量、构造方法和成员方法都放在方法区中,new出来的对象都放在堆里,接收对象的变量都放在栈里,存放的是堆里对象的地址。所以输出s时,输出的是s所指向的对象的地址,可以通过成员方法的调用取出数据。

ArrayList集合

1.我们学习的面向对象编程语言中,对事物的描述都是通过对象来体现,堆里方便对多个对象进行操作,我们必须对对个对象进行存储,这样就不能是一个基本的变量,而是容器类型的变量。
StringBuilder的结果只能是一个字符串类型,不一定能满足需求。
对象数组的长度是固定的,如果有时候元素的个数不确定,就无法定义出数组的长度,因此提供了ArrayList集合类。
2.集合类ArrayList是在java.util包中,因此需要导包。是一个长度大小可变数组的实现。
3.ArrayList的方法:

  • 构造方法:ArrayList()
  • 添加方法:public boolean add(E e):添加元素
    public void add(int index,E element):在指定的索引处添加一个元素
public static void main(String[] args) {
		//创建集合对象
		ArrayList<String> array = new ArrayList<String>();	
		//添加元素:public boolean add(E e)
		array.add("hello");
		array.add("world");
		array.add("zhongguo");
		//public void add(int index,E element):在指定的索引处添加一个元素
		array.add(2, "nihao");
		System.out.println("array:"+array);
}

输出结果:

array:[hello, world, nihao, zhongguo]
  • 获取元素: public E get(int index):返回指定索引处的元素 ,不改变集合
public static void main(String[] args) {
		//创建集合对象
		ArrayList<String> array = new ArrayList<String>();	
		//添加元素
		array.add("hello");
		array.add("world");
		array.add("zhongguo");
		//public E get(int index):返回指定索引处的元素
		System.out.println("get:"+array.get(0));
		System.out.println("get:"+array.get(1));
		System.out.println("get:"+array.get(2));
		System.out.println("array:"+array);
	}

输出结果:

get:hello
get:world
get:zhongguo
array:[hello, world, zhongguo]
  • 集合长度: public int size():返回集合中的元素的个数,不改变集合
public static void main(String[] args) {
		//创建集合对象
		ArrayList<String> array = new ArrayList<String>();		
		//添加元素
		array.add("hello");
		array.add("world");
		array.add("zhongguo");		
		//public int size():返回集合中的元素的个数
		System.out.println("size:"+array.size());
}

输出结果:

size:3
  • 删除元素: public boolean remove(Object o):删除指定的元素,返回删除是否成功 ,删除成功改变集合。
    public E remove(int index):删除指定索引处的元素,返回被删除的元素,删除成功改变集合。
public static void main(String[] args) {
		//创建集合对象
		ArrayList<String> array = new ArrayList<String>();		
		//添加元素
		array.add("hello");
		array.add("world");
		array.add("zhongguo");		
		System.out.println("array=="+array);
		//public boolean remove(Object o):删除指定的元素,返回删除是否成功
		System.out.println("remove:"+array.remove("world"));//true
		System.out.println("array:"+array);	
		System.out.println("remove:"+array.remove("world"));//false
	    System.out.println("array:"+array);	
		//public E remove(int index):删除指定索引处的元素,返回被删除的元素
		System.out.println("remove:"+array.remove(0));
		System.out.println("array="+array);
}

输出结果:

array==[hello, world, zhongguo]
remove:true
array:[hello, zhongguo]
remove:false
array:[hello, zhongguo]
remove:hello
array=[zhongguo]
  • 修改元素: public E set(int index,E element):修改指定索引处的元素,返回被修改的元素,改变集合。
public static void main(String[] args) {
		//创建集合对象
		ArrayList<String> array = new ArrayList<String>();		
		//添加元素
		array.add("hello");
		array.add("world");
		array.add("zhongguo");		
		System.out.println("array=="+array);
		//public E set(int index,E element):修改指定索引处的元素,返回被修改的元素
		System.out.println("set:"+array.set(0, "你好"));		
		//输出
		System.out.println("array:"+array);
	}

输出结果:

array==[hello, world, zhongguo]
set:hello
array:[你好, world, zhongguo]

注意: < E >是一个特殊的类型,泛型。在出现E的地方使用引用数据类型替换即可。
4.遍历ArrayList
要求:将字符串数组中的元素添加到集合中,并且把姓王的打印出来

public static void main(String[] args) {
		String[] str = {"张三","李四","王五","王柳","赵六"};
		//创建集合对象
		ArrayList<String> array = new ArrayList<String>();		
		//遍历数组,增加到集合类中
		for(int i =0;i<str.length;i++){
			array.add(str[i]);
		}
		//输出集合类中全部的
		System.out.println("array="+array);
		//遍历集合类,输出姓王的
		System.out.println("输出姓王的:");
		for(int i =0;i<array.size();i++){
			String s = array.get(i);
			if(s.startsWith("王")){//字符串以什么开头
				//输出
				System.out.println(s);
			}
		}				
	}

输出结果:

array=[张三, 李四, 王五, 王柳, 赵六]
输出姓王的:
王五
王柳

猜你喜欢

转载自blog.csdn.net/w_e_i_1201/article/details/83079029
今日推荐