Explanation of the difference between ArrayList collection class and object array case

Table of contents

Object array case explanation

 gather

Constructor and member methods of ArrayList class

Object array case explanation

  • Create a student array, store three student objects and traverse
package com.demo01;
/**
 * 
 * @author 无限嚣张菜菜
 * 分析:
 * 	A:定义学生类
 * 	B:创建学生数组
 * 	C:创建学生对象
 * 	D:把学生对象作为元素赋值给学生数组
 * 	E:遍历学生数组
 *
 */

public class StudentDemo {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		// 创建数组对象,创建的是一个类对象
		student[] students = new student[3];
		
		// 创建学生对象,初始值
		student s1 = new student("张三",26);
		student s2 = new student("李四",28);
		student s3 = new student("王五",30);
		
		//把学生对象作为元素赋值为学生数组
		students[0] = s1;
		students[1] = s2;
		students[2] = s3;
		
		//遍历学生数组,students 是一个数组对象
		for(int i=0;i<3;i++) {
			student s = students[i];
			System.out.println(s.getName()+"---------"+s.getAge());
		}

	}

}

 gather

Why do collection classes appear?

What we are learning is an object-oriented language, and the description of things in an object-oriented language is embodied by objects. In order to facilitate the operation of multiple objects, we must store these multiple objects. If you want to store multiple objects, it cannot be a basic variable, but a container type variable. Among the knowledge we have learned so far, what are the container types??Array andStringBuilder. But what? ? StringBuilder The result is a string , which may not satisfy Our requirement,So we can only choose an array, which is the object array. The object array cannot adapt to changing needs, because the length of the array is fixed. At this time, in order to adapt to changing needs, < a i=13>Java provides collection classes for us to use. It can be seen that the length of the set is variable.

ArrayListConstructor and member methods of class

  • Construction method

        –ArrayList()

  • member method

        –Additional elements 

        –Capturing element

        –Aggregation length

        –element removed

        –Modification element

package com.demo01;

import java.util.ArrayList;

/*
 * 获取元素
 * 		public E get(int index):返回指定索引处的元素
 * 集合长度
 * 		public int size():返回集合中元素的个数
 * 删除元素
 * 		public boolean remove(Object o):删除指定元素,返回删除是否成功
 * 		public E remove(int index):删除指定索引处的元素,返回被删除的元素
 * 修改元素
 * 		public E set(int index,E element):修改指定索引处的元素,返回被修改的元素
 */
public class ArrayListDemo2 {
	public static void main(String[] args) {
		// 创建集合列表
		ArrayList<String> array = new ArrayList<String>();
		
		//增加元素
		array.add("hello");
		array.add("world");
		array.add("java");
		
		//public E get(int index):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));
		
		//public int size():返回集合中元素的个数
		System.out.println("size:"+array.size());
		
		//public boolean remove(Object o):删除指定元素,返回删除是否成功
		System.out.println("remove:"+array.remove("world"));//true
		System.out.println(array);
		System.out.println("remove:"+array.remove("world"));//false
		
		//public E remove(int index):删除指定索引处的元素,返回被删除的元素
		System.out.println("remove:"+array.remove(1));
		System.out.println(array);
		array.add("zy");
		//public E set(int index,E element):修改指定索引处的元素,返回被修改的元素
		System.out.println("set:"+array.set(1, "android"));
		
		//输出最后结果
		System.out.println("array:"+array);
	}
}

Difference:ArrayListThe class can also be regarded as an array object, but it is an array object with variable length.

Guess you like

Origin blog.csdn.net/zywcxz/article/details/128815401