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

ArrayList를 함수 인수로 전달하는 Java 프로그램

<시간/>

이 기사에서는 ArrayList를 함수 인수로 전달하는 방법을 이해할 것입니다. ArrayListclass는 java.util 패키지에서 찾을 수 있는 크기 조정이 가능한 배열입니다. Java에서 내장 배열과 ArrayList의 차이점은 배열의 크기를 수정할 수 없다는 것입니다.

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

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

Run the program

원하는 출력은 -

The list is defined as:
Java Python Scala Mysql Redshift

알고리즘

Step 1 - START
Step 2 - Declare namely
Step 3 - Define the values.
Step 4 - Create an ArrayList, and iterate over it, and display it.
Step 5 - In the main method, create the ArrayList, and add elements to it using the ‘add’ method.
Step 6 - Display this on the console.
Step 7 - Stop

예시 1

여기에서 문자열 배열 목록을 반복합니다.

import java.util.ArrayList;
public class Demo {
   public static void print(ArrayList<String> input_list) {
      System.out.print("\nThe list is defined as:\n ");
      for(String language : input_list) {
         System.out.print(language + " ");
      }
   }
   public static void main(String[] args) {
      System.out.println("The required packages have been imported");
      ArrayList<String> input_list = new ArrayList<>();
      input_list.add("Java");
      input_list.add("Python");
      input_list.add("Scala");
      input_list.add("Mysql");
      input_list.add("Redshift");
      print(input_list);
   }
}

출력

The required packages have been imported

The list is defined as:
Java Python Scala Mysql Redshift

예시 2

여기에서 정수 배열 목록을 반복합니다.

import java.util.ArrayList;
public class Demo {
   public static void print(ArrayList<String> input_list) {
      System.out.print("\nThe list is defined as:\n ");
      for(String language : input_list) {
         System.out.print(language + " ");
      }
   }
   public static void main(String[] args) {
      System.out.println("The required packages have been imported");
      ArrayList<String> input_list = new ArrayList<>();
      input_list.add("Java");
      input_list.add("Python");
      input_list.add("Scala");
      input_list.add("Mysql");
      input_list.add("Redshift");
      print(input_list);
   }
}

출력

The required packages have been imported

The list is defined as:
Java Python Scala Mysql Redshift