码迷,mamicode.com
首页 > 编程语言 > 详细

Java 数组

时间:2016-06-08 23:00:51      阅读:240      评论:0      收藏:0      [点我收藏+]

标签:

1. 声明数组 

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

2. 输出数组 

int[] intArray = { 1, 2, 3, 4, 5 };  
String intArrayString = Arrays.toString(intArray);  

3. 数组-->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]  

4.  检查一个数组是否包含某个值 

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

5.  连接两个数组 

int[] intArray = { 1, 2, 3, 4, 5 };  
int[] intArray2 = { 6, 7, 8, 9, 10 };  
// Apache Commons Lang library  
int[] combinedIntArray = ArrayUtils.addAll(intArray, intArray2); 

6. String[] --> String

// Apache common lang  
String j = StringUtils.join(new String[] { "a", "b", "c" }, ", ");  
System.out.println(j);   // a, b, c  

7. ArrayList --> 数组

String[] stringArray = { "a", "b", "c", "d", "e" };  
ArrayList<String> arrayList = new ArrayList<String>(Arrays.asList(stringArray));  
// ArrayList<String> --> String[] String[] stringArr
= new String[arrayList.size()]; arrayList.toArray(stringArr);
return stringArr;
// or this way
String[] stringArr = (String[])arrayList.toArray(new String[0]);
return stringArr;

8. 数组 --> Set

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

9. reverse 数组 

int[] intArray = { 1, 2, 3, 4, 5 };  
ArrayUtils.reverse(intArray);  

10. 移除数组中的元素

int[] intArray = { 1, 2, 3, 4, 5 };  
int[] removed = ArrayUtils.removeElement(intArray, 3);//create a new array  

 

Java 数组

标签:

原文地址:http://www.cnblogs.com/hesier/p/5571392.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!