📜  Java中的多维集合

📅  最后修改于: 2022-05-13 01:54:25.348000             🧑  作者: Mango

Java中的多维集合

在Java中,我们有一个 Collection 框架,它提供了存储一组对象的功能。这称为一维 ArrayList,其中一行中只能有一个元素。极客,但是如果我们想要创建一个多维 ArrayList 怎么办,对于我们在Java中确实有多维集合(或嵌套集合)的这个功能。

多维集合(或嵌套集合)是一组对象的集合,其中每个组可以动态地拥有任意数量的对象。因此,在这里我们可以随时在一个组中存储任意数量的元素。

Multidimensional_Collections_in_Java

插图:

Single dimensional ArrayList :
  [121, 432, 12, 56, 456, 3, 1023]
  [Apple, Orange, Pear, Mango]

句法:

ArrayList  x = new ArrayList ();

需要多维集合

与数组不同,我们不受多维集合中任何行的大小的限制。因此,如果我们想使用多维架构,可以连续动态创建任意数量的对象,那么我们应该使用Java中的多维集合。

语法 多维集合

ArrayList> a = new ArrayList>();

插图:

Multidimensional ArrayList: [[3, 4], [12, 13, 14, 15], [22, 23, 24], [33]]

让我们快速浏览一下多维 ArrayList 的 add() 方法,如下所示:

  • boolean add(ArrayList e) :用于在指定集合中插入元素。
  • void add( int index, ArrayList e) :用于在集合中的指定位置插入元素。

    示例 1:

    Java
    // Java Program to Illustrate Multidimensional ArrayList
     
    // Importing required classes
    import java.util.*;
     
    // Main class
    // MultidimensionalArrayList
    class GFG {
     
        // Method 1
        // To create and return 2D ArrayList
        static List create2DArrayList()
        {
     
            // Creating a 2D ArrayList of Integer type
            ArrayList > x
                = new ArrayList >();
     
            // One space allocated for R0
            x.add(new ArrayList());
     
            // Adding 3 to R0 created above x(R0, C0)
            x.get(0).add(0, 3);
     
            // Creating R1 and adding values
            // Note: Another way for adding values in 2D
            // collections
            x.add(
                new ArrayList(Arrays.asList(3, 4, 6)));
     
            // Adding 366 to x(R1, C0)
            x.get(1).add(0, 366);
     
            // Adding 576 to x(R1, C4)
            x.get(1).add(4, 576);
     
            // Now, adding values to R2
            x.add(2, new ArrayList<>(Arrays.asList(3, 84)));
     
            // Adding values to R3
            x.add(new ArrayList(
                Arrays.asList(83, 6684, 776)));
     
            // Adding values to R4
            x.add(new ArrayList<>(Arrays.asList(8)));
     
            // Appending values to R4
            x.get(4).addAll(Arrays.asList(9, 10, 11));
     
            // Appending values to R1, but start appending from
            // C3
            x.get(1).addAll(3, Arrays.asList(22, 1000));
     
            // This method will return 2D array
            return x;
        }
     
        // Method 2
        // Main driver method
        public static void main(String args[])
        {
            // Display message prior for better readability
            System.out.println("2D ArrayList :");
     
            // Printing 2D ArrayList by calling Method 1
            System.out.println(create2DArrayList());
        }
    }


    Java
    // Java Program to Illustrate Multidimensional LinkedHashSet
     
    // Importing required classes
    import java.util.*;
     
    // Main class
    // Multidimensional LinkedHashSet
    class GFG {
     
        // Method 1
        // To create and return 2D LinkedHashSet
        static Set create2DLinkedHashSet()
        {
            // Creating an empty 2D LinkedHashSet
            LinkedHashSet > x
                = new LinkedHashSet >();
     
            // Creating R0
            x.add(new LinkedHashSet(
                Arrays.asList("Apple", "Orange")));
     
            // Creating R1, here "Coffee" will be considered as
            // only one object to maintain uniqueness
            x.add(new LinkedHashSet(Arrays.asList(
                "Tea", "Coffee", "Milk", "Coffee", "Water")));
     
            // Creating R2
            x.add(new LinkedHashSet(
                Arrays.asList("Tomato", "Potato", "Onion")));
     
            // Creating R3 but it will not be added as it
            // contains the same items as R2
     
            // Note: LinkedHashSet inserts only unique items
     
            x.add(new LinkedHashSet(
                Arrays.asList("Tomato", "Potato", "Onion")));
     
            // Returning multidimensional LinkedHashSet
            return x;
        }
     
        // Method 2
        // Main driver method
        public static void main(String[] args)
        {
            // Display message for better readability
            System.out.println("2D LinkedHashSet :");
     
            // Printing 2D LinkedHashSet by
            // calling method 1
            System.out.println(create2DLinkedHashSet());
        }
    }


    输出
    2D ArrayList :
    [[3], [366, 3, 4, 22, 1000, 6, 576], [3, 84], [83, 6684, 776], [8, 9, 10, 11]]

    现在让我们看看多维 LinkedHashSet 的相同实现,并展示它的不同行为方式。同样,我们可以将任何其他集合实现为多维集合,如下所示:

    句法:

    HashSet< HashSet > a = new HashSet< HashSet >(); 

    示例 2:

    Java

    // Java Program to Illustrate Multidimensional LinkedHashSet
     
    // Importing required classes
    import java.util.*;
     
    // Main class
    // Multidimensional LinkedHashSet
    class GFG {
     
        // Method 1
        // To create and return 2D LinkedHashSet
        static Set create2DLinkedHashSet()
        {
            // Creating an empty 2D LinkedHashSet
            LinkedHashSet > x
                = new LinkedHashSet >();
     
            // Creating R0
            x.add(new LinkedHashSet(
                Arrays.asList("Apple", "Orange")));
     
            // Creating R1, here "Coffee" will be considered as
            // only one object to maintain uniqueness
            x.add(new LinkedHashSet(Arrays.asList(
                "Tea", "Coffee", "Milk", "Coffee", "Water")));
     
            // Creating R2
            x.add(new LinkedHashSet(
                Arrays.asList("Tomato", "Potato", "Onion")));
     
            // Creating R3 but it will not be added as it
            // contains the same items as R2
     
            // Note: LinkedHashSet inserts only unique items
     
            x.add(new LinkedHashSet(
                Arrays.asList("Tomato", "Potato", "Onion")));
     
            // Returning multidimensional LinkedHashSet
            return x;
        }
     
        // Method 2
        // Main driver method
        public static void main(String[] args)
        {
            // Display message for better readability
            System.out.println("2D LinkedHashSet :");
     
            // Printing 2D LinkedHashSet by
            // calling method 1
            System.out.println(create2DLinkedHashSet());
        }
    }
    
    输出
    2D LinkedHashSet :
    [[Apple, Orange], [Tea, Coffee, Milk, Water], [Tomato, Potato, Onion]]