public class ArrayListDemo {
public static void main(String[] args) {
/*
* 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);
// 直接打印,则会打印出引用对象的Hash值
// [I@7150bd4d
System.out.println(intArray);
// [1, 2, 3, 4, 5]
System.out.println(intArrayString);
/*
* 3. 根据数组创建ArrayList
* */
String[] stringArray = { "a", "b", "c", "d", "e" };
ArrayList
// [a, b, c, d, e]
System.out.println(arrayList);
/*
* 4. 检查数组是否包含某个值
* */
String[] stringArray2 = { "a", "b", "c", "d", "e" };
boolean b = Arrays.asList(stringArray2).contains("a");
// true
System.out.println(b);
/*
* 5. 合并连接两个数组
* */
int[] intArray1 = { 1, 2, 3, 4, 5 };
int[] intArray2 = { 6, 7, 8, 9, 10 };
// Apache Commons Lang 库
int[] combinedIntArray = ArrayUtils.addAll(intArray1, intArray2);
/*
* 6. 用给定的字符串连结(join)数组
* */
// containing the provided list of elements
// Apache common lang
String j = StringUtils.join(new String[] { "a", "b", "c" }, ", ");
// a, b, c
System.out.println(j);
/*
* 7. 将ArrayList转换为数组
* */
String[] stringArray3 = { "a", "b", "c", "d", "e" };
ArrayList
String[] stringArr = new String[arrayList4.size()];
arrayList4.toArray(stringArr);
for (String s : stringArr)
System.out.println(s);
/*
* 8. 将数组转换为Set
* */
Set
//[d, e, b, c, a]
System.out.println(set);
/*
* 9. 数组元素反转
* */
int[] intArray4 = { 1, 2, 3, 4, 5 };
ArrayUtils.reverse(intArray4);
//[5, 4, 3, 2, 1]
System.out.println(Arrays.toString(intArray4));
/*
* 10. 移除元素
* */
int[] intArray6 = { 1, 2, 3, 4, 5 };
int[] removed = ArrayUtils.removeElement(intArray6, 3);//创建新的数组
System.out.println(Arrays.toString(removed));
}
}