Java数组应用十大技巧攻略
The following are top 10 methods for Java Array. They are the most voted questions from stackoverflow.(关于Java数组最顶级的11大应用方法,这些方法在工作流程问题中经常会用到!无论是运用在团队环境或是在私人项目中,你都可以直接拿来用!)
0. Declare an array(声明数组)
String[] aArray = new String[5];
String[] bArray = {"a","b","c", "d", "e"};
String[] cArray = new String[]{"a","b","c","d","e"};
1. Print an array in Java(数组的输出)
package org.test;
import java.util.Arrays;
public class Test1 {
public static void main(String args[]) {
int[] intArray = { 1, 2, 3, 4, 5 };
//数组输出,方法一
int i=0;
for(i=0;i<5;i++){
System.out.println(intArray[i]);
}
//数组输出,方法二
for (int shuzu : intArray)
System.out.println(shuzu);
String intArrayString = Arrays.toString(intArray);
// print directly will print reference value
System.out.println(intArray);
// [I@7150bd4d
System.out.println(intArrayString);
// [1, 2, 3, 4, 5]
}
}
2. Create an ArrayList from an array(从数组中创建ArrayList数组列表)
String[] stringArray = { "a", "b", "c", "d", "e" };
ArrayList<String> arrayList = new ArrayList<String>(Arrays.asList(stringArray));
System.out.println(arrayList);//输出: [a, b, c, d, e]
3. Check if an array contains a certain value(检查数组中是否包含特定值)
String[] stringArray = { "a", "b", "c", "d", "e" };
boolean b = Arrays.asList(stringArray).contains("a");
System.out.println(b);// true
4. Concatenate two arrays(连接两个数组)
int[] intArray = { 1, 2, 3, 4, 5 };
int[] intArray2 = { 6, 7, 8, 9, 10 };
// Apache Commons Lang library(ArrayUtils是Apache提供的class)
int[] combinedIntArray = ArrayUtils.addAll(intArray, intArray2);
5. Declare an array inline(声明一个数组内链)
method(new String[]{"a", "b", "c", "d", "e"});
6. Joins the elements of the provided array into a single String(将数组元素加入到一个独立的字符串中)
// containing the provided list of elements
// Apache common lang
String j = StringUtils.join(new String[] { "a", "b", "c" }, ", ");
System.out.println(j);
// a, b, c
7. Covnert an ArrayList to an array(将数组列表转换成一个数组)
String[] stringArray = { "a", "b", "c", "d", "e" };
ArrayList<String> arrayList = new ArrayList<String>(Arrays.asList(stringArray));
String[] stringArr = new String[arrayList.size()];
arrayList.toArray(stringArr);
for (String s : stringArr)
System.out.println(s);
8. Convert an array to a set(将数组转换成一个集合)
Set<String> set = new HashSet<String>(Arrays.asList(stringArray));
System.out.println(set);//[d, e, b, c, a]
9. Reverse an array (反向数组)
int[] intArray = { 1, 2, 3, 4, 5 };
ArrayUtils.reverse(intArray);
System.out.println(Arrays.toString(intArray));//[5, 4, 3, 2, 1]
10. Remove element of an array(删除数组元素)
int[] intArray = { 1, 2, 3, 4, 5 };
int[] removed = ArrayUtils.removeElement(intArray, 3);//create a new array
System.out.println(Arrays.toString(removed));
One more – convert int to byte array ()
byte[] bytes = ByteBuffer.allocate(4).putInt(8).array();
for (byte t : bytes) {
System.out.format("0x%x ", t);
}
java数组的初始化:
1.动态初始化:数组定义与为数组分配空间和赋值的操作分开进行;
2.静态初始化:在定义数字的同时就为数组元素分配空间并赋值;
3.默认初始化:数组是引用类型,它的元素相当于类的成员变量,因此数组分配空间后,每个元素也被按照成员变量的规则被隐士初始化。
实例:
TestD.java(动态):
package org.test;
public class TestD {
public static void main(String args[]) {
int a[];
a = new int[3];
a[0] = 0;
a[1] = 1;
a[2] = 2;
Date days[];
days = new Date[3];
days[0] = new Date(2008, 4, 5);
days[1] = new Date(2008, 2, 31);
days[2] = new Date(2008, 4, 4);
}
}
class Date {
int year, month, day;
Date(int year, int month, int day) {
this.year = year;
this.month = month;
this.day = day;
}
}
TestS.java(静态):
程序代码:
<span style="font-size:12px">public class TestS {
public static void main(String args[]) {
int a[] = { 0, 1, 2 };
Time times[] = { new Time(19, 42, 42), new Time(1, 23, 54),
new Time(5, 3, 2) };
}
}
class Time {
int hour, min, sec;
Time(int hour, int min, int sec) {
this.hour = hour;
this.min = min;
this.sec = sec;
}
}</span>
TestDefault.java(默认):
程序代码:
<span style="font-size:12px">public class TestDefault {
public static void main(String args[]) {
int a[] = new int[5];
System.out.println("" + a[3]);
}
}</span>
java.util.Arrays类
此类包含用来操作数组(比如排序和搜索)的各种方法。此类还包含一个允许将数组作为列表来查看的静态工厂。 除非特别注明,否则如果指定数组引用为 null,则此类中的方法都会抛出 NullPointerException。
包含下述常用方法:
-
toString 将一个数组转换成String,用于打印数组
isEquals 判断两个数组是否相等,采用EqualsBuilder进行判断
toMap 将一个数组转换成Map,如果数组里是Entry则其Key与Value就是新Map的Key和Value,如果是Object[]则Object[0]为KeyObject[1]为Value
clone 拷贝数组
subarray 截取子数组
isSameLength 判断两个数组长度是否相等
getLength 获得数组的长度
isSameType 判段两个数组的类型是否相同
reverse 数组反转
indexOf 查询某个Object在数组中的位置,可以指定起始搜索位置
lastIndexOf 反向查询某个Object在数组中的位置,可以指定起始搜索位置
contains 查询某个Object是否在数组中
toObject 将基本数据类型转换成外包型数据
isEmpty 判断数组是否为空(null和length=0的时候都为空)
addAll 合并两个数组
add 添加一个数据到数组
remove 删除数组中某个位置上的数据
removeElement 删除数组中某个对象(从正序开始搜索,删除第一个)
参考:http://www.programcreek.com/2013/09/top-10-methods-for-java-arrays/