java ArrayList 使用心得与总结

在用 c 语言 或 C++ 时,我遇到的最大麻烦就是动态数组的内存释放了,经常容易出错。

而 java 的 ArrayList 类 很好的解决了这个问题,它类似于 C++ 中的容器类 vector,自带很多方法对数组操作。

详细的介绍可以参看 oracle 的文档:

ArrayList (Java Platform SE 8 )

1. 构造方法:

(1)ArrayList()

构造一个空的数组,默认容量是 10

(2)ArrrayList(int initialCapacity)

构造一个指定容量的空数组,当增加数据导致容量不足时,容量默认增加上次容量大小的一半

(3)ArrayList(Collection<? extends E> c)

这一点不是很好理解,它其实表示生成一个与括号内数组一样的另一个数组,括号内的数组能够继承生成后的数组。 

使用时

 Arraylist<Object> list=new Arraylist<Object> ();

<>以及里面的内容可以省略,但编译器会给出提示,最好还是带上。

println 可以直接打印整个数组,举例:

public class ArrayListTest {
	class Human{
	}
	
	class Male extends Human{	
	}
	
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		ArrayList<Integer> list1=new ArrayList<Integer>();
		list1.add(1);
		list1.add(2);
		list1.add(3);
		System.out.println(list1);
		
		ArrayList<Integer> list2=new ArrayList<Integer>(list1);
		System.out.println(list2);
		
		
		ArrayList<Male>list3=new ArrayList<Male>();
		ArrayList<Human>list4=new ArrayList<Human>(list3);
		
		System.out.println(list3);
		System.out.println(list4);		
	}
}

输出:

[1, 2, 3]
[1, 2, 3]
[]
[]

2. ArrayList 中常用的方法

add(E e):                     在数组末尾添加元素

size():                           数组中实际元素个数,并不是数组容量

add(int index, E e):    在数组指定位置添加元素

clear():                         将数组中元素清空

contains(E e):             判断数组中是否含有某个元素

get(int index):             返回数组指定位置的元素

indexOf(E e):              返回数组指定元素第一次出现的位置

set(int index, E e):     替换数组指定位置的值

remove(int index):     移除数组指定位置的元素

remove(E e):              移除数组中第一次出现的指定元素

addAll(Collection<? extends E> c):                     在数组末尾添加另一个数组 

addAll(int index, collection<? extends E> c):     在数组指定位置添加另一个数组 

removeAll(Collection<?>c):                                   将数组中属于数组 c 中的元素全部删除

举例:

import java.util.ArrayList;
import java.util.Arrays;


public class ArrayListTest {
	class Human{
	}
	
	class Male extends Human{	
	}
	
	public static void main(String[] args) {
		ArrayList<Integer> list1=new ArrayList<Integer>();
		list1.add(1); // Appends the specified element to the end of this list
		list1.add(2);
		list1.add(3);
		System.out.println(list1.size());
		System.out.println(list1);
		
		list1.add(2,4); // Inserts the specified element at the specified position in this list
		System.out.println(list1);
		
		list1.clear(); // Removes all of the elements from this list
		System.out.println(list1);
		
		list1.add(5);
		ArrayList<Integer> list2=new ArrayList<Integer>();
		list2.add(1);
		list2.add(2);
		list2.add(3);
		list2.addAll(list1); // Appends all of the elements in the specified collection to the end of this list
		System.out.println(list2);
		
		System.out.println(list2.contains(5)); // Judge if this list contains the specified element
		System.out.println(list2.get(2)); // Returns the element at the specified position in this list
		System.out.println(list2.indexOf(5)); // Returns the index of the first occurrence of the specified element, or -1 if this list doesn't contain
		
		list2.set(2, 10); // Replaces the element at the specified position in this list with the specified element
		System.out.println(list2);
		
		list2.remove(2); // Removes the element at the specified position in this list
		System.out.println(list2); 
		list2.remove(Integer.valueOf(1)); // Removes the first occurrence of the specified element from this list, if it is present
		System.out.println(list2);
		list2.removeAll(Arrays.asList(5)); // Removes from this list all of its elements that are contained in the specified collection
	}
}

输出结果:

3
[1, 2, 3]
[1, 2, 4, 3]
[]
[1, 2, 3, 5]
true
3
3
[1, 2, 10, 5]
[1, 2, 5]
[2, 5]
 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

心态与习惯

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值