-
배열 선언
String[] aArray = new String[5]; String[] bArray = {"a","b","c", "d", "e"}; String[] cArray = new String[]{"a","b","c","d","e"};
-
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]
-
배열에서 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]
-
특정 값에 대한 배열 확인
String[] stringArray = { "a", "b", "c", "d", "e" }; boolean b = Arrays.asList(stringArray).contains("a"); System.out.println(b); // true
-
두 배열 병합
int[] intArray = { 1, 2, 3, 4, 5 }; int[] intArray2 = { 6, 7, 8, 9, 10 }; // Apache Commons Lang library int[] combinedIntArray = ArrayUtils.addAll(intArray, intArray2);
-
한 줄에 배열 선언하기
method(new String[]{"a", "b", "c", "d", "e"});
-
배열 요소를 문자열로 연결
// 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
-
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);
-
배열을 집합으로 변환(
Set
)Set<String> set = new HashSet<String>(Arrays.asList(stringArray)); System.out.println(set); //[d, e, b, c, a]
-
요소가 역순으로 포함된 배열을 반환합니다.
int[] intArray = { 1, 2, 3, 4, 5 }; ArrayUtils.reverse(intArray); System.out.println(Arrays.toString(intArray)); //[5, 4, 3, 2, 1]
-
배열에서 요소 제거
int[] intArray = { 1, 2, 3, 4, 5 }; int[] removed = ArrayUtils.removeElement(intArray, 3);//create a new array System.out.println(Arrays.toString(removed));
byte
그리고 한 가지 더 - 유형의 값을 기반으로 유형의 배열을 생성합니다int
(대략 takeByteBuffer
, 그 안에 4바이트를 선택하고 숫자int
8을 입력한 다음 이 모든 것(0, 0, 0, 8)을 배열로 변환합니다.byte
통화를 사용하는 유형array()
)byte[] bytes = ByteBuffer.allocate(4).putInt(8).array(); for (byte t : bytes) { System.out.format("0x%x ", t); }
자바에서 일반적으로 사용되는 배열 기술 10가지
Java에서는 다음과 같은 10가지 배열 트릭이 일반적으로 사용됩니다. 프로그래밍에 관한 질문과 답변 시스템인 스택 오버플로(Stack Overflow)에서 가장 높은 순위를 차지했습니다.
GO TO FULL VERSION