Computer >> 컴퓨터 >  >> 프로그램 작성 >> Java

목록을 두 부분으로 나누는 Java 프로그램

<시간/>

이 기사에서는 목록을 두 부분으로 나누는 방법을 이해할 것입니다. 목록은 요소를 순차적으로 저장하고 액세스할 수 있도록 하는 정렬된 컬렉션입니다. 여기에는 요소를 삽입, 업데이트, 삭제 및 검색하는 인덱스 기반 메서드가 포함되어 있습니다. 중복 요소를 가질 수도 있습니다.

아래는 동일한 데모입니다 -

입력이 다음과 같다고 가정 -

Input list :[Java, Python, JavaScript, Shell, Scala]

원하는 출력은 -

The first half of the list is:
[Java, Python]
The second half of the list is:
[JavaScript, Shell, Scala]

알고리즘

Step 1 - START
Step 2 - Declare three array list namely input_list, first_list, second_list.
Step 3 - Define the values.
Step 4 - Get the size of the array using the function .size().
Step 5 - Iterate the input_list using a for-loop, add all the elements with index lesser the size/2 index value to the first_list and add all the elements with index greater the size/2 index value to the second_list using the .add() function.
Step 6 - Display the result
Step 7 - Stop

예시 1

여기에서 모든 작업을 'main' 기능 아래에 묶습니다.

import java.util.ArrayList;
import java.util.List;
public class Demo {
   public static void main(String[] args) {
      System.out.println("Required packages have been imported");
      List<String> input_list = new ArrayList<String>();
      input_list.add("Java");
      input_list.add("Python");
      input_list.add("JavaScript");
      input_list.add("Shell");
      input_list.add("Scala");
      System.out.println("The list is defined as " +input_list);
      List<String> first_list = new ArrayList<String>();
      List<String> second_list = new ArrayList<String>();
      int size = input_list.size();
      System.out.println("\nThe first half of the list is: ");
      for (int i = 0; i < size / 2; i++)
         first_list.add(input_list.get(i));
      System.out.println(first_list);
      System.out.println("The second half of the list is: ");
      for (int i = size / 2; i < size; i++)
         second_list.add(input_list.get(i));
      System.out.println(second_list);
   }
}

출력

Required packages have been imported
The list is defined as [Java, Python, JavaScript, Shell, Scala]

The first half of the list is:
[Java, Python]
The second half of the list is:
[JavaScript, Shell, Scala]

예시 2

여기에서 객체 지향 프로그래밍을 나타내는 함수로 작업을 캡슐화합니다.

import java.util.ArrayList;
import java.util.List;
public class Demo {
   static void split_list(List<String> input_list) {
      List<String> first_list = new ArrayList<String>();
      List<String> second_list = new ArrayList<String>();
      int size = input_list.size();
      System.out.println("\nThe first half of the list is: ");
      for (int i = 0; i < size / 2; i++)
         first_list.add(input_list.get(i));
      System.out.println(first_list);
      System.out.println("The second half of the list is: ");
      for (int i = size / 2; i < size; i++)
         second_list.add(input_list.get(i));
      System.out.println(second_list);
   }
   public static void main(String[] args) {
      System.out.println("Required packages have been imported");
      List<String> input_list = new ArrayList<String>();
      input_list.add("Java");
      input_list.add("Python");
      input_list.add("JavaScript");
      input_list.add("Shell");
      input_list.add("Scala");
      System.out.println("The list is defined as " +input_list);
      split_list(input_list);
   }
}

출력

Required packages have been imported
The list is defined as [Java, Python, JavaScript, Shell, Scala]

The first half of the list is:
[Java, Python]
The second half of the list is:
[JavaScript, Shell, Scala]