본문 바로가기
웹프로그래밍/Java

Java Array

by Seras 2017. 11. 22.
반응형


0. 배열 선언하기

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

1. Java로 배열 인쇄

int[] intArray = { 1, 2, 3, 4, 5 };
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. 배열에서 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. 배열에 특정 값이 들어 있는지 확인하십시오

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

4. 두 배열을 연결합니다.

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

5. 배열을 인라인으로 선언하십시오.

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

6. 제공된 배열의 요소를 단일 문자열로 조인합니다.

// 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. ArrayList를 배열에 할당합니다.

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. 배열을 집합으로 변환

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

9. 배열 반전

int[] intArray = { 1, 2, 3, 4, 5 };
ArrayUtils.reverse(intArray);
System.out.println(Arrays.toString(intArray));
//[5, 4, 3, 2, 1]

10. 배열 요소를 제거하십시오.

int[] intArray = { 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);
}


반응형

'웹프로그래밍 > Java' 카테고리의 다른 글

Java Collections  (0) 2017.11.22
Java Strings  (0) 2017.11.22
Java HashMap의 자주 사용되는 메소드  (0) 2017.11.22
Request, Response객체  (0) 2017.11.22
[ERROR] Ambiguous mapping found. Cannot map  (0) 2017.11.15