JavaRush /Java Blog /Random-KO /자바에서 일반적으로 사용되는 배열 기술 10가지
theGrass
레벨 24
Саратов

자바에서 일반적으로 사용되는 배열 기술 10가지

Random-KO 그룹에 게시되었습니다
Java에서는 다음과 같은 10가지 배열 트릭이 일반적으로 사용됩니다. 프로그래밍에 관한 질문과 답변 시스템인 스택 오버플로(Stack Overflow)에서 가장 높은 순위를 차지했습니다. 자바에서 일반적으로 사용되는 배열 기술 10가지 - 1
  1. 배열 선언

    String[] aArray = new String[5];
    String[] bArray = {"a","b","c", "d", "e"};
    String[] cArray = new String[]{"a","b","c","d","e"};
  2. 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]
  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");
    System.out.println(b);
    // true
  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. 한 줄에 배열 선언하기

    method(new String[]{"a", "b", "c", "d", "e"});
  7. 배열 요소를 문자열로 연결

    // 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
  8. 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);
  9. 배열을 집합으로 변환( Set)

    Set<String> set = new HashSet<String>(Arrays.asList(stringArray));
    System.out.println(set);
    //[d, e, b, c, a]
  10. 요소가 역순으로 포함된 배열을 반환합니다.

    int[] intArray = { 1, 2, 3, 4, 5 };
    ArrayUtils.reverse(intArray);
    System.out.println(Arrays.toString(intArray));
    //[5, 4, 3, 2, 1]
  11. 배열에서 요소 제거

    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(대략 take ByteBuffer, 그 안에 4바이트를 선택하고 숫자 int8을 입력한 다음 이 모든 것(0, 0, 0, 8)을 배열로 변환합니다. byte통화를 사용하는 유형 array())

    byte[] bytes = ByteBuffer.allocate(4).putInt(8).array();
    
    for (byte t : bytes) {
       System.out.format("0x%x ", t);
    }
코멘트
TO VIEW ALL COMMENTS OR TO MAKE A COMMENT,
GO TO FULL VERSION