logo

Java에서 목록을 배열로 변환

그만큼 목록 순서가 지정된 컬렉션을 저장하는 데 사용되는 널리 사용되는 컬렉션 인터페이스 중 하나입니다. List 인터페이스는 요소의 삽입 순서를 유지하고 중복된 값도 저장할 수 있습니다.

이번 섹션에서는 List를 Array로 변환하는 방법을 알아 보겠습니다. Java에서는 주로 다음과 같이 목록을 배열로 변환하는 세 가지 방법이 있습니다.

  1. List의 get() 메소드 사용
  2. toArray() 메소드 사용
  3. Java 8에서 스트림 사용

get() 메소드 사용

목록을 배열로 변환하는 가장 간단한 방법 중 하나입니다. 이런 방식으로 모든 목록 요소에 하나씩 액세스하여 배열에 추가합니다.

대칭차

구문은 얻다() List 인터페이스의 메소드는 다음과 같습니다.

 public Ele get(int pos) 

get() 메서드는 목록의 지정된 위치에 있는 요소를 반환합니다.

어떻게 사용하는지 이해하기 위해 목록을 배열로 변환하는 예를 들어보겠습니다. 얻다() 목록의 방법.

ConvertListToArrayExample1.java

 import java.io.*; import java.util.LinkedList; import java.util.List; // create ConvertListToArrayExample1 class to convert a list into an array class ConvertListToArrayExample1 { // main() method start public static void main(String[] args) { // create linked list by declaring an object of List List names = new LinkedList(); // use add() method of the list to add elements in the linked list names.add(&apos;Paul&apos;); names.add(&apos;Donal&apos;); names.add(&apos;James&apos;); names.add(&apos;Robert&apos;); names.add(&apos;Mery&apos;); // get size of list and store it into len variable int len = names.size(); // declare and initialize array of type string to store list elements String[] namesArray = new String[ len ]; // iterate list using for loop and add all the elements into namesArray one by one to convert names list into an array for (int i = 0; i <len; i++) namesarray[i]="names.get(i);" print all the elements of array system.out.println('after converting list into an array'); for (int j="0;" < namesarray.length; j++) { system.out.println((j+1)+' element is '+namesarray[j]); } pre> <p> <strong>Output:</strong> </p> <img src="//techcodeview.com/img/java-tutorial/59/convert-list-array-java.webp" alt="Convert List to Array in Java"> <h2>Using toArray() Method</h2> <p>It is another way of converting a list into an array. By using the toArray() method, we can easily convert a list into an array. The toArray() method returns an array having all the elements in the list. The returned array contains elements in the same sequence as the list.</p> <p>The syntax of the <strong>toArray()</strong> method of the List interface is as follows:</p> <pre> public T[] toArray(T[] a) </pre> <p>The toArray() method either accepts an array as a parameter or no parameter, and it returns an array containing all the elements in the list.</p> <p>Let&apos;s take another example of converting a list into an array to understand how we can use the <strong>toArray()</strong> method of the list.</p> <p> <strong>ConvertListToArrayExample2.java</strong> </p> <pre> import java.io.*; import java.util.LinkedList; import java.util.List; //create ConvertListToArrayExample2 class to convert a list into an array class ConvertListToArrayExample2 { // main() method start public static void main(String[] args) { // create linked list by declaring an object of List List names = new LinkedList(); // use add() method of the list to add elements in the linked list names.add(&apos;Paul&apos;); names.add(&apos;Donal&apos;); names.add(&apos;James&apos;); names.add(&apos;Robert&apos;); names.add(&apos;Mery&apos;); // use toArray() method to convert a list into an array String[] namesArray = names.toArray(new String[0]); // print all the elements of the array System.out.println(&apos;After converting List into an Array&apos;); for (int j = 0; j <namesarray.length; j++) { system.out.println((j+1)+' element of the array is '+namesarray[j]); } < pre> <p> <strong>Output:</strong> </p> <img src="//techcodeview.com/img/java-tutorial/59/convert-list-array-java-2.webp" alt="Convert List to Array in Java"> <h2>Using Stream</h2> <p>There is one more way of converting a List into an array, i.e., by using Stream introduced in Java8.</p> <p>The syntax of the <strong>toArray()</strong> method of the List interface is as follows:</p> <pre> public T[] toArray(T[] a) </pre> <p>The toArray() method either accepts an array as a parameter or no parameter. It returns an array containing all the elements in the list.</p> <p>Let&apos;s take another example of converting a list into an array to understand how we can use the <strong>toArray()</strong> method of the list.</p> <p> <strong>ConvertListToArrayExample3.java</strong> </p> <pre> import java.io.*; import java.util.LinkedList; import java.util.List; //create ConvertListToArrayExample3 class to convert a list into an array class ConvertListToArrayExample3 { // main() method start public static void main(String[] args) { // create linked list by declaring an object of List List names = new LinkedList(); // use add() method of the list to add elements in the linked list names.add(&apos;Paul&apos;); names.add(&apos;Donal&apos;); names.add(&apos;James&apos;); names.add(&apos;Robert&apos;); names.add(&apos;Mery&apos;); // use stream() method to convert a list into an array String[] namesArray = names.stream().toArray(String[] ::new); // print all the elements of the array System.out.println(&apos;After converting List into an Array&apos;); for (int j = 0; j <namesarray.length; j++) { system.out.println((j+1)+' element of the array is '+namesarray[j]); } < pre> <p> <strong>Output:</strong> </p> <img src="//techcodeview.com/img/java-tutorial/59/convert-list-array-java-3.webp" alt="Convert List to Array in Java"> <p>In Java, we mostly use get() and toArray() methods for converting a list into an array. The stream() method is not efficient in comparison of get() and toArray() methods.</p> <hr></namesarray.length;></pre></namesarray.length;></pre></len;>

toArray() 메서드는 배열을 매개 변수로 받아들이거나 매개 변수 없이 받아들이며 목록의 모든 요소가 포함된 배열을 반환합니다.

어떻게 사용하는지 이해하기 위해 목록을 배열로 변환하는 또 다른 예를 살펴보겠습니다. toArray() 목록의 방법.

ConvertListToArrayExample2.java

 import java.io.*; import java.util.LinkedList; import java.util.List; //create ConvertListToArrayExample2 class to convert a list into an array class ConvertListToArrayExample2 { // main() method start public static void main(String[] args) { // create linked list by declaring an object of List List names = new LinkedList(); // use add() method of the list to add elements in the linked list names.add(&apos;Paul&apos;); names.add(&apos;Donal&apos;); names.add(&apos;James&apos;); names.add(&apos;Robert&apos;); names.add(&apos;Mery&apos;); // use toArray() method to convert a list into an array String[] namesArray = names.toArray(new String[0]); // print all the elements of the array System.out.println(&apos;After converting List into an Array&apos;); for (int j = 0; j <namesarray.length; j++) { system.out.println((j+1)+\' element of the array is \'+namesarray[j]); } < pre> <p> <strong>Output:</strong> </p> <img src="//techcodeview.com/img/java-tutorial/59/convert-list-array-java-2.webp" alt="Convert List to Array in Java"> <h2>Using Stream</h2> <p>There is one more way of converting a List into an array, i.e., by using Stream introduced in Java8.</p> <p>The syntax of the <strong>toArray()</strong> method of the List interface is as follows:</p> <pre> public T[] toArray(T[] a) </pre> <p>The toArray() method either accepts an array as a parameter or no parameter. It returns an array containing all the elements in the list.</p> <p>Let&apos;s take another example of converting a list into an array to understand how we can use the <strong>toArray()</strong> method of the list.</p> <p> <strong>ConvertListToArrayExample3.java</strong> </p> <pre> import java.io.*; import java.util.LinkedList; import java.util.List; //create ConvertListToArrayExample3 class to convert a list into an array class ConvertListToArrayExample3 { // main() method start public static void main(String[] args) { // create linked list by declaring an object of List List names = new LinkedList(); // use add() method of the list to add elements in the linked list names.add(&apos;Paul&apos;); names.add(&apos;Donal&apos;); names.add(&apos;James&apos;); names.add(&apos;Robert&apos;); names.add(&apos;Mery&apos;); // use stream() method to convert a list into an array String[] namesArray = names.stream().toArray(String[] ::new); // print all the elements of the array System.out.println(&apos;After converting List into an Array&apos;); for (int j = 0; j <namesarray.length; j++) { system.out.println((j+1)+\' element of the array is \'+namesarray[j]); } < pre> <p> <strong>Output:</strong> </p> <img src="//techcodeview.com/img/java-tutorial/59/convert-list-array-java-3.webp" alt="Convert List to Array in Java"> <p>In Java, we mostly use get() and toArray() methods for converting a list into an array. The stream() method is not efficient in comparison of get() and toArray() methods.</p> <hr></namesarray.length;></pre></namesarray.length;>

toArray() 메서드는 배열을 매개변수로 받아들이거나 매개변수를 받아들이지 않습니다. 목록의 모든 요소를 ​​포함하는 배열을 반환합니다.

어떻게 사용하는지 이해하기 위해 목록을 배열로 변환하는 또 다른 예를 살펴보겠습니다. toArray() 목록의 방법.

ConvertListToArrayExample3.java

 import java.io.*; import java.util.LinkedList; import java.util.List; //create ConvertListToArrayExample3 class to convert a list into an array class ConvertListToArrayExample3 { // main() method start public static void main(String[] args) { // create linked list by declaring an object of List List names = new LinkedList(); // use add() method of the list to add elements in the linked list names.add(&apos;Paul&apos;); names.add(&apos;Donal&apos;); names.add(&apos;James&apos;); names.add(&apos;Robert&apos;); names.add(&apos;Mery&apos;); // use stream() method to convert a list into an array String[] namesArray = names.stream().toArray(String[] ::new); // print all the elements of the array System.out.println(&apos;After converting List into an Array&apos;); for (int j = 0; j <namesarray.length; j++) { system.out.println((j+1)+\' element of the array is \'+namesarray[j]); } < pre> <p> <strong>Output:</strong> </p> <img src="//techcodeview.com/img/java-tutorial/59/convert-list-array-java-3.webp" alt="Convert List to Array in Java"> <p>In Java, we mostly use get() and toArray() methods for converting a list into an array. The stream() method is not efficient in comparison of get() and toArray() methods.</p> <hr></namesarray.length;>