1. 程式人生 > >數組的各種應用

數組的各種應用

tin 聲明 tint alloc ear reat () 應用 byte

public class Test03 {

/**
* @param args
*/
public static void main(String[] args) {
//聲明數組一個長度為5的數組
String[] aArray = new String[5];
System.out.println(aArray.length);
//聲明自定義內容數組,內容的多少決定了數組的長度
String[] bArray = {"a","b","c"};
String bArrayString = Arrays.toString(bArray);//Arrays.toString是調用的一個方法,然後可以輸出這個數組
System.out.println(bArrayString);
//聲明數組
String[] cArray = new String[]{"a","b","c","d"};
String cArrayString = Arrays.toString(cArray);
System.out.println(cArrayString);
//從一個數組創建數組列表
String[] dArray = {"a","b","c","d","e"};
ArrayList<String> arrayList = new ArrayList<String>(Arrays.asList(dArray));
System.out.println(arrayList);
//檢查一個數組是否包含某個值
String[] eArray = {"a","b","c","d","e"};
boolean contains = Arrays.asList(eArray).contains("f");
System.out.println(contains);
//連接倆個數組
String[] aint = {"1","2","3","4"};
String[] bint = {"5","6","7","8"};
//int[] addAll = ArrayUtils.addAll(aint, bint);
//System.out.println(addAll);
ArrayList<String> strList = new ArrayList<String>();
strList.add(Arrays.toString(aint));
strList.add(Arrays.toString(bint));
System.out.println(strList);
//把提供的數組元素放入一個字符串
//String join = StringUtils.join(new String[] {"a","b","c"}, ",");
//System.out.println(join);
//將一個數組列表轉換為數組
String[] stringArray = {"a","b","c","d","e"};
ArrayList<String> arrayList2 = new ArrayList<String>(Arrays.asList(stringArray));
String[] strarr =new String[arrayList2.size()];
arrayList2.toArray(strarr);
for(String s : strarr){
System.out.print(s+" ");
}
System.out.println();
//將一個數組轉換為集
String[] s1 = {"a","b","c","d","e"};
HashSet<String> set = new HashSet<String>(Arrays.asList(s1));
System.out.println(set);
//逆向一個數組
int [] i1={1,2,3,4,5};
//ArrayUtils.reverse(i1);
//System.out.println(Arrays.toString(i1));
//移除數組中的元素
int[] i2={1,2,3,4,5};
//int[] removed = ArrayUtils.removeElement(intArray, 3);//create a new array
//System.out.println(Arrays.toString(removed));
//將整數換為字節數組
byte[] bytes = ByteBuffer.allocate(4).putInt(8).array();
for(byte t : bytes){
System.out.format("0x%x ", t);
}

}

}

數組的各種應用