一、静态数组的使用方法与实例

数组属于引用数据类型,如下,把intArray传递给anotherArray,会发生引用传递。即改变anotherArray的值的同使也会改变intArray的值,因为intArray和anotherArray都是指向同一块内存空间。

int[] intArray = { 1, 2, 3, 4, 5 };
//引用传递
int[] anotherArray = intArray;
//改变anotherArray第一个元素的值
anotherArray[0] = 0;
//打印intArray的值
System.out.println(Arrays.toString(intArray));//{ 0, 2, 3, 4, 5 }
//打印anotherArray的值
System.out.println(Arrays.toString(anotherArray));//{ 0, 2, 3, 4, 5 }

如果只想把int的值传递给新的数组,不想引发引用传递,可以用clone():

int[] intArray = { 1, 2, 3, 4, 5 };
//值传递
int[] anotherArray = intArray.clone();
//改变anotherArray第一个元素的值
anotherArray[0] = 0;
//打印intArray的值
System.out.println(Arrays.toString(intArray));//{ 1, 2, 3, 4, 5 }
//打印anotherArray的值
System.out.println(Arrays.toString(anotherArray));//{ 0, 2, 3, 4, 5 }

 

·静态数组的声明

String[] aArray;
aArray = new String[3];
        
String[] bArray = new String[5];
        
String[] cArray = {"a","b","c"};
        
String[] dArray = new String[]{"a","b","c"};

·打印int数组(要先转为String)

int[] intArray = { 1, 2, 3, 4, 5 };
String intArrayString = Arrays.toString(intArray);
//直接打印int数组
System.out.println(intArray);// [I@7150bd4d

System.out.println(intArrayString);// [1, 2, 3, 4, 5]

·静态数组转动态数组ArrayList

String[] stringArray = { "a", "b", "c", "d", "e" };
ArrayList<String> arrayList = new ArrayList<>(Arrays.asList(stringArray));
System.out.println(arrayList);// [a, b, c, d, e]

·检查数组是否包含特定的值

String[] stringArray = { "a", "b", "c", "d", "e" };
boolean b = Arrays.asList(stringArray).contains("a");
System.out.println(b);// true

·判断两个数组是否相等(必须是相同数据类型的数组)

int[] intArray = { 1, 2, 3, 4, 5 };
int[] intArray2 = { 1, 2, 3, 4, 6 };
boolean b = Arrays.equals(intArray,intArray2);
System.out.println(b);//false

·将数组的元素全部变为同一个值

int[] intArray = { 1, 2, 3, 4, 5 };
Arrays.fill(intArray,6);
System.out.println(Arrays.toString(intArray)); //{6, 6, 6, 6, 6}

·将数组的元素按顺序排序

int[] intArray = { 6, 2, 5, 4, 5 };
Arrays.sort(intArray);
System.out.println(Arrays.toString(intArray)); //{2, 4, 5, 5, 6}

·int转成byte[]

//将整型30转成长度为4的byte数组
byte[] bytes = ByteBuffer.allocate(4).putInt(30).array();

for (byte t : bytes) {
     System.out.format("0x%x ", t);
}

二、动态数组ArrayList的使用方法与实例

ArrayList类是一个特殊的数组。通过添加和删除元素,就可以动态改变数组的长度。优点是可以灵活地插入或删除元素,缺点是速度比一般数组要慢。

· 创建动态数组有下面两种方法,想知道其中的区别可看:

List list = new ArrayList();

ArrayList arrayList = new ArrayList();

· 从下面看出,ArrayList也是属于引用类型:

ArrayList<String> arrayList = new ArrayList<>();
arrayList.add("a");
arrayList.add("b");

ArrayList<String> arrayList2 = arrayList;
arrayList2.remove("a");

Log.i(TAG, String.valueOf(arrayList));// [b]
Log.i(TAG, String.valueOf(arrayList2));// [b]

· 跟数组一样,使用clone()传递值:

ArrayList<String> arrayList = new ArrayList<>();
arrayList.add("a");
arrayList.add("b");
        
ArrayList<String> arrayList2 = (ArrayList<String>) arrayList.clone();
arrayList2.remove("a");

Log.i(TAG, String.valueOf(arrayList));// [a, b]
Log.i(TAG, String.valueOf(arrayList2));// [b]

 

三、List <E>

其中E是泛型,数据类型可以是String、Boolean、Integer、Byte、Short、Long、Float、Double、Charater等类型。但不接受基本数据类型boolean、byte、short、int、long、float、double、char。

·新建String类型的List

List<String> list= new ArrayList<>();

·获取List中元素的数量

list.size();

·检查List中是否为空

list.isEmpty();

·检查List中是否包含指定元素

list.contains("h");//入参是Object

·转成数组Array-1

Object[] myArray = list.toArray();//结果是Object[]
String[] x = (String[]) myArray;

·转成数组Array-2

String[] y = list.toArray(new String[3]);

·添加元素到队列末尾

list.add("hey");

·把元素插入到指定位置

list.add(3,"amberoot");

·删除指定元素

list.remove("hey");

·根据索引删除元素

list.remove(0);//删除list中的第一个元素

·添加另外一个Collection的所有元素(所有继承于接口Collection的对象,List和ArrayList都继承于该接口)

List secondList = new ArrayList();
list.addAll(secondList);

·获取指定元素的索引值

list.indexOf("amberoot");