java数组的常用方法实例源码介绍



java数组的常用方法实例源码介绍。将两个相同类型的数组连接起来,填充元素、比较大小、复制元素,将List集合转换成数组,拷贝数组,将数组转换成Set集合,字符串输出数组等。

java数组的声明方法:

作为一种引用类型、就如我们平常使用引用类型的时候声明一样、一般有两种写法:

 

1
2
a) type[] arrayName;    exp: String[] strArray;
b) type arrayName[];    exp: String strArray[];

 第二种源于C的写法、由于很容易造成混淆、所以现在基本不使用这种声明方式了。

2、java数组的初始化:

java数组的初始化方法有两种:

a) 静态初始化——数组大小由系统分配、我们只为数组每个位置上赋值

 

1
2
String[] strArray1 = {"a", "b", "c", "d", "e"};
String[] strArray2 = new String[]{"a", "b", "c", "d", "e"};//在 new String[]中不能指定String数组的大小!

b)动态初始化——只指定数值的大小、初始化工作由系统为我们完成(即为数组的每个位置赋初始值)

1
2
3
4
5
6
7
String[] strArray3 = new String[5];//此时String数组的每个位置上的值都由系统来初始化、使用默认的值""
//我们能做的是动态的为strArray3每个位置上的值进行修改
for (int i = 0; i < strArray1.length; i++) {
    //这里仅用原始的方法进行赋值。
    strArray3[i] = strArray1[i];
}

java数组的常用方法实例:

package com.chy.array.usefulMethods;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashSet;
import java.util.Set;

import com.chy.array.bean.Student;

@SuppressWarnings(“all”)
public class ArrayUseFulMethoed {

private static int[] intArray = {1, 2, 3, 4, 5};
private static String[] strArray = {“a”, “b”, “c”, “d”, “e”};

/**
* 填充元素、比较大小、复制元素
*/
public static void testFillArray(){
//注意字符串和对象的不同
Student[] student1 = new Student[4];
Student[] student2 = new Student[4];
System.out.println(Arrays.equals(student1, student2));//true

Arrays.fill(student1, 0, 4, new Student(1,”chy”));
Arrays.fill(student2, new Student(1,”chy”));
System.out.println(Arrays.equals(student1, student2));//false

String[] str1 = new String[4];
String[] str2 = new String[]{“a”, “a”, “a”, “a”};
String[] str3 = {new String(“a”), new String(“a”), new String(“a”), new String(“a”)};
Arrays.fill(str1, “a”);
System.out.println(Arrays.equals(str1, str2));//true
System.out.println(Arrays.equals(str2, str3));//true

String[] str4 = Arrays.copyOf(str1, 2);//是将传入的数组拷贝len个元素到新的数组、相当于复制本身的一部分或者全部形成一个全新的数组
System.out.println(str4.length + “=======” + Arrays.toString(str4));// 2=======[a, a]

String[] str5 = new String[8];
System.arraycopy(str4, 0, str5, 6, 2);//是将str4从下标0开的2个元素拷贝到从下标6开始放置的数组str5中
System.out.println(str5.length + “=======” + Arrays.toString(str5));// 8=======[null, null, null, null, null, null, a, a]


}

/**
* 以字符串的形式输出指定数组的“模样”
*/
public static void printOriginalArray(){
String intArrayToString = Arrays.toString(intArray);
System.out.println(intArrayToString); //result: [1, 2, 3, 4, 5]
}

/**
* 将数组转化成List集合
* 注意:不能直接将int[]转化为集合、因为asList()方法的参数必须是对象。应该先把int[]转化为Integer[]。
* 对于其他primitive类型的数组也是如此,必须先转换成相应的wrapper类型数组。
*/
public static void convetArrayToList(){
Integer[] integerArray = new Integer[intArray.length];
for (int i = 0; i < integerArray.length; i++) {
integerArray[i] = intArray[i];
}
ArrayList<integer> integerList1 = new ArrayList<integer>(Arrays.asList(integerArray));

/*
* 不能写成下面:
* ArrayList<integer> integerList2 = (ArrayList<integer>)Arrays.asList(integerArray);
* 返回的是List<t>、强转可以通过编译、但是不能正常使用。
*/
for(int i : integerList1){
System.out.print(i);
}
//result: 12345
System.out.println();

}

/**
* 将List集合转换成数组
*/
public static void convetListToArray(){

ArrayList<string> strList = new ArrayList<string>(Arrays.asList(strArray));
String[] strArrayFromList = new String[strList.size()];
strList.toArray(strArrayFromList);
System.out.println(Arrays.toString(strArrayFromList)); //result: [a, b, c, d, e]

/*
* 注意:不能写成这样:String[] strArrayFromList = (String[])strList.toArray(strArrayFromList);会抛出ClassCastException。
* List.toArray()与List.toArray(T[] t)的区别在于:
* List.toArray()返回的是一个Object[]、不能强转成String[]、强转的话可以通过编译、但是不能进行String[]的操作
* 而List.toArray(T[] t)会将list的值转换成T类型的数组。
*/

}
/**
* 将数组转换成Set集合
*/
public static void convertArrayToSet(){
Set<string> set = new HashSet<string>(Arrays.asList(strArray));
//Set具有无序性、所以输出结构不一定是原来数组元素存放顺序
System.out.println(set); //result: [d, e, b, c, a]
}

/**
* 判断某个数组中是否包含一个元素、思路:将数组转换成list使用list的contains方法
*/
public static void isContainObject(){
ArrayList<string> strList = new ArrayList<string>(Arrays.asList(strArray));
System.out.println(strList.contains(“a”)); //result: true

//另一种实现
Arrays.sort(strArray);
if(Arrays.binarySearch(strArray, “c”) >= 0){
System.out.println(true);
}else{
System.out.println(false);
}
}

/**
* 将两个相同类型的数组连接起来
*/
public static void connTwoSameArray(){
int[] intArray2 = new int[]{6, 7, 8, 9, 10};
}

/**
* 将数组中数据排序
*/
public static void sortArray(){
String[] str = {“c”, “a” ,”d” ,”z” };
Arrays.sort(str);
System.out.println(Arrays.toString(str));

//反序、
Arrays.sort(str, Collections.reverseOrder());
System.out.println(Arrays.toString(str));

}

public static void main(String[] args) {
/*printOriginalArray();
convetArrayToList();
convetListToArray();
isContainObject();
convertArrayToSet();
sortArray();*/
testFillArray();

}
}