copyOf() 메소드 사용
copyOf() Arrays 클래스(java.util 패키지)의 메소드는 두 개의 매개변수를 받아들입니다 -
-
배열(모든 유형).
-
길이를 나타내는 정수 값입니다.
그리고 주어진 배열의 내용을 시작 위치에서 주어진 길이로 복사하고 새로운 배열을 반환합니다.
예시
import java.util.Arrays;
public class CopyingSectionOfArray {
public static void main(String[] args) {
String str[] = new String[10];
//Populating the array
str[0] = "Java";
str[1] = "WebGL";
str[2] = "OpenCV";
str[3] = "OpenNLP";
str[4] = "JOGL";
str[5] = "Hadoop";
str[6] = "HBase";
str[7] = "Flume";
str[8] = "Mahout";
str[9] = "Impala";
System.out.println("Contents of the Array: \n"+Arrays.toString(str));
String[] newArray = Arrays.copyOf(str, 5);
System.out.println("Contents of the copies array: \n"+Arrays.toString(newArray));
}
} 출력
Contents of the Array: [Java, WebGL, OpenCV, OpenNLP, JOGL, Hadoop, HBase, Flume, Mahout, Impala] Contents of the copies array: [Java, WebGL, OpenCV, OpenNLP, JOGL]
copyOfRange() 메소드 사용
copyOfRange() Arrays 클래스의 메소드(java.util 패키지)는 세 개의 매개변수를 받아들입니다 -
-
배열(모든 유형)
-
배열의 시작과 끝 위치를 나타내는 두 개의 정수 값.
그리고 지정된 범위에서 주어진 배열의 내용을 복사하고 새 배열을 반환합니다.
예시
import java.util.Arrays;
public class CopyingSectionOfArray {
public static void main(String[] args) {
String str[] = new String[10];
//Populating the array
str[0] = "Java";
str[1] = "WebGL";
str[2] = "OpenCV";
str[3] = "OpenNLP";
str[4] = "JOGL";
str[5] = "Hadoop";
str[6] = "HBase";
str[7] = "Flume";
str[8] = "Mahout";
str[9] = "Impala";
System.out.println("Contents of the Array: \n"+Arrays.toString(str));
String[] newArray = Arrays.copyOfRange(str, 2, 7);
System.out.println("Contents of the copies array: \n"+Arrays.toString(newArray));
}
} 출력
Contents of the Array: [Java, WebGL, OpenCV, OpenNLP, JOGL, Hadoop, HBase, Flume, Mahout, Impala] Contents of the copies array: [OpenCV, OpenNLP, JOGL, Hadoop, HBase]