Computer >> Máy Tính >  >> Lập trình >> Java

Chương trình Java để in một bộ sưu tập

Trong bài này, chúng ta sẽ hiểu cách in một bộ sưu tập. Bộ sưu tập là một khuôn khổ cung cấp kiến ​​trúc để lưu trữ và thao tác với nhóm các đối tượng. Bộ sưu tập Java lưu trữ tất cả các thao tác mà bạn thực hiện trên dữ liệu như tìm kiếm, sắp xếp, chèn, thao tác và xóa.

Dưới đây là một minh chứng về điều tương tự -

Giả sử đầu vào của chúng tôi là -

Run the program

Đầu ra mong muốn sẽ là -

The Elements of the collection are:
Language : Java | Language_id : 101
Language : Scala | Language_id : 102
Language : Python | Language_id : 103
Language : Mysql | Language_id : 104

Thuật toán

Step 1 - START
Step 2 - Declare a collection namely input_list
Step 3 - Define the values.
Step 4 - Create objects namely object_1 , object_2, object_3, object_4 and with each object , add a key value pair to the collection.
Step 5 - Using a for-each loop, display the elements of the collection
Step 6 - Stop

Ví dụ 1

Ở đây, chúng tôi liên kết tất cả các hoạt động với nhau trong hàm "main".

import java.util.*;
public class Demo {
   String name;
   int id;
   Demo(String s, int n){
      name = s;
      id = n;
   }
   public String toString(){
      return "Language : " + name + " | Language_id : " + id;
   }
   static void print(ArrayList<Demo> input_array){
      System.out.println("The Elements of the collection are: ");
      for (Demo element : input_array)
         System.out.println(element);
   }
   public static void main(String[] args){
      ArrayList<Demo> input_array = new ArrayList<Demo>();
      Demo object_1 = new Demo("Java", 101);
      Demo object_2 = new Demo("Scala", 102);
      Demo object_3 = new Demo("Python", 103);
      Demo object_4 = new Demo("Mysql", 104);
      input_array.add(object_1);
      input_array.add(object_2);
      input_array.add(object_3);
      input_array.add(object_4);
      print(input_array);
   }
}

Đầu ra

The Elements of the collection are:
Language : Java | Language_id : 101
Language : Scala | Language_id : 102
Language : Python | Language_id : 103
Language : Mysql | Language_id : 104

Ví dụ 2

Ở đây, chúng tôi đóng gói các hoạt động thành các hàm thể hiện lập trình hướng đối tượng.

import java.util.*;
public class Demo {
   String name;
   int id;
   Demo(String s, int n){
      name = s;
      id = n;
   }
   public String toString(){
      return "Language : " + name + " | Language_id : " + id;
   }
   static void print(ArrayList<Demo> input_array){
      System.out.println("The Elements of the collection are: ");
      for (Demo element : input_array)
         System.out.println(element);
   }
   public static void main(String[] args){
      ArrayList<Demo> input_array = new ArrayList<Demo>();
      Demo object_1 = new Demo("Java", 101);
      Demo object_2 = new Demo("Scala", 102);
      Demo object_3 = new Demo("Python", 103);
      Demo object_4 = new Demo("Mysql", 104);
      input_array.add(object_1);
      input_array.add(object_2);
      input_array.add(object_3);
      input_array.add(object_4);
      print(input_array);
   }
}

Đầu ra

The Elements of the collection are:
Language : Java | Language_id : 101
Language : Scala | Language_id : 102
Language : Python | Language_id : 103
Language : Mysql | Language_id : 104