Java数组应用十大技巧攻略

                        Java数组应用十大技巧攻略

        The following are top 10 methods for Java Array. They are the most voted questions from stackoverflow.(关于Java数组最顶级的11大应用方法,这些方法在工作流程问题中经常会用到!无论是运用在团队环境或是在私人项目中,你都可以直接拿来用!)

0. Declare an array(声明数组

  1. String[] aArray = new String[5];  
  2. String[] bArray = {"a","b","c""d""e"};  
  3. String[] cArray = new String[]{"a","b","c","d","e"};   
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(数组的输出

  1. package org.test;  
  2. import java.util.Arrays;  
  3. public class Test1 {  
  4.     public static void main(String args[]) {  
  5.         int[] intArray = { 12345 };    
  6.         //数组输出,方法一  
  7.         int i=0;  
  8.         for(i=0;i<5;i++){  
  9.         System.out.println(intArray[i]);  
  10.         }  
  11.         //数组输出,方法二  
  12.         for (int shuzu : intArray)  
  13.             System.out.println(shuzu);   
  14.         String intArrayString = Arrays.toString(intArray);    
  15.         // print directly will print reference value     
  16.         System.out.println(intArray);  
  17.         // [I@7150bd4d     
  18.         System.out.println(intArrayString);    
  19.         // [1, 2, 3, 4, 5]  
  20.     }  
  21. }  
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数组列表

  1. String[] stringArray = { "a""b""c""d""e" };  
  2. ArrayList<String> arrayList = new ArrayList<String>(Arrays.asList(stringArray));  
  3. System.out.println(arrayList);//输出: [a, b, c, d, e]   
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(检查数组中是否包含特定值)

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

4. Concatenate two arrays(连接两个数组)

  1. int[] intArray = { 12345 };  
  2. int[] intArray2 = { 678910 };  
  3. // Apache Commons Lang library(ArrayUtils是Apache提供的class)  
  4. int[] combinedIntArray = ArrayUtils.addAll(intArray, intArray2);   
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(声明一个数组内链)

  1. method(new String[]{"a""b""c""d""e"});   
method(new String[]{"a", "b", "c", "d", "e"}); 

6. Joins the elements of the provided array into a single String(将数组元素加入到一个独立的字符串中)

  1. // containing the provided list of elements  
  2. // Apache common lang  
  3. String j = StringUtils.join(new String[] { "a""b""c" }, ", ");  
  4. System.out.println(j);  
  5. // a, b, c   
// 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(将数组列表转换成一个数组)

  1. String[] stringArray = { "a""b""c""d""e" };  
  2. ArrayList<String> arrayList = new ArrayList<String>(Arrays.asList(stringArray));  
  3. String[] stringArr = new String[arrayList.size()];  
  4. arrayList.toArray(stringArr);  
  5. for (String s : stringArr)  
  6.     System.out.println(s);   
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(将数组转换成一个集合)

  1. Set<String> set = new HashSet<String>(Arrays.asList(stringArray));  
  2. System.out.println(set);//[d, e, b, c, a]   
Set<String> set = new HashSet<String>(Arrays.asList(stringArray));
System.out.println(set);//[d, e, b, c, a] 

9. Reverse an array (反向数组)

  1. int[] intArray = { 12345 };  
  2. ArrayUtils.reverse(intArray);  
  3. System.out.println(Arrays.toString(intArray));//[5, 4, 3, 2, 1]   
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(删除数组元素

  1. int[] intArray = { 12345 };  
  2. int[] removed = ArrayUtils.removeElement(intArray, 3);//create a new array  
  3. System.out.println(Arrays.toString(removed));   
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 ()

  1. byte[] bytes = ByteBuffer.allocate(4).putInt(8).array();  
  2.  for (byte t : bytes) {  
  3.    System.out.format("0x%x ", t);  
  4. }   
byte[] bytes = ByteBuffer.allocate(4).putInt(8).array();
 for (byte t : bytes) {
   System.out.format("0x%x ", t);
} 

 

java数组的初始化:

1.动态初始化:数组定义与为数组分配空间和赋值的操作分开进行;
2.静态初始化:在定义数字的同时就为数组元素分配空间并赋值;
3.默认初始化:数组是引用类型,它的元素相当于类的成员变量,因此数组分配空间后,每个元素也被按照成员变量的规则被隐士初始化

实例:

TestD.java(动态):

  1. package org.test;  
  2. public class TestD {  
  3.     public static void main(String args[]) {  
  4.         int a[];  
  5.         a = new int[3];  
  6.         a[0] = 0;  
  7.         a[1] = 1;  
  8.         a[2] = 2;  
  9.         Date days[];  
  10.         days = new Date[3];  
  11.         days[0] = new Date(200845);  
  12.         days[1] = new Date(2008231);  
  13.         days[2] = new Date(200844);  
  14.     }  
  15. }  
  16. class Date {  
  17.     int year, month, day;  
  18.   
  19.     Date(int year, int month, int day) {  
  20.         this.year = year;  
  21.         this.month = month;  
  22.         this.day = day;  
  23.     }  
  24. }  
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(静态):

程序代码:

  1. <span style="font-size:12px">public class TestS {  
  2.     public static void main(String args[]) {  
  3.         int a[] = { 012 };  
  4.         Time times[] = { new Time(194242), new Time(12354),  
  5.                 new Time(532) };  
  6.     }  
  7. }  
  8. class Time {  
  9.     int hour, min, sec;  
  10.   
  11.     Time(int hour, int min, int sec) {  
  12.         this.hour = hour;  
  13.         this.min = min;  
  14.         this.sec = sec;  
  15.     }  
  16. }</span>  
<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(默认):

程序代码:

  1. <span style="font-size:12px">public class TestDefault {  
  2.     public static void main(String args[]) {  
  3.         int a[] = new int[5];  
  4.         System.out.println("" + a[3]);  
  5.     }  
  6. }</span>  
<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/

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值