Java ArrayList 的数组?

发布于 2024-09-17 16:30:33 字数 216 浏览 3 评论 0原文

我想创建一个没有固定大小的多维数组。

我需要能够向其中添加 String[2] 项目。

我尝试过查看:

private ArrayList<String[]> action = new ArrayList<String[2]>();

但这不起作用。还有人有其他想法吗?

I want to create a mutli dimensional array without a fixed size.

I need to be able to add items of String[2] to it.

I have tried looking at:

private ArrayList<String[]> action = new ArrayList<String[2]>();

but that doesn't work. does anyone have any other ideas?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

孤独患者 2024-09-24 16:30:33

应该是

private ArrayList<String[]> action = new ArrayList<String[]>();
action.add(new String[2]);
...

你不能在泛型参数中指定数组的大小,只能稍后将特定大小的数组添加到列表中。这也意味着编译器不能保证所有子数组的大小相同,必须由您来保证。

更好的解决方案可能是将其封装在一个类中,您可以在其中确保数组大小作为类型不变式的统一大小。

Should be

private ArrayList<String[]> action = new ArrayList<String[]>();
action.add(new String[2]);
...

You can't specify the size of the array within the generic parameter, only add arrays of specific size to the list later. This also means that the compiler can't guarantee that all sub-arrays be of the same size, it must be ensured by you.

A better solution might be to encapsulate this within a class, where you can ensure the uniform size of the arrays as a type invariant.

如若梦似彩虹 2024-09-24 16:30:33

顺便提一句。您应该更喜欢针对接口进行编码。

private ArrayList<String[]> action = new ArrayList<String[]>();

应该是

private List<String[]> action = new ArrayList<String[]>();

BTW. you should prefer coding against an Interface.

private ArrayList<String[]> action = new ArrayList<String[]>();

Should be

private List<String[]> action = new ArrayList<String[]>();
远昼 2024-09-24 16:30:33

由于字符串数组的大小在编译时是固定的,因此最好使用仅要求两个字段的结构(如 Pair),从而避免数组方法可能出现的运行时错误。

代码

由于 Java 不提供 Pair 类,因此您需要定义自己的类。

class Pair<A, B> {
  public final A first;
  public final B second;

  public Pair(final A first, final B second) {
    this.first = first;
    this.second = second;
  }

  //
  // Override 'equals', 'hashcode' and 'toString'
  //
}

然后将其用作:

List<Pair<String, String>> action = new ArrayList<Pair<String, String>>();

[ 这里我使用 List 因为它被认为是对接口进行编程的良好实践。 ]

Since the size of your string array is fixed at compile time, you'd be better off using a structure (like Pair) that mandates exactly two fields, and thus avoid the runtime errors possible with the array approach.

Code:

Since Java doesn't supply a Pair class, you'll need to define your own.

class Pair<A, B> {
  public final A first;
  public final B second;

  public Pair(final A first, final B second) {
    this.first = first;
    this.second = second;
  }

  //
  // Override 'equals', 'hashcode' and 'toString'
  //
}

and then use it as:

List<Pair<String, String>> action = new ArrayList<Pair<String, String>>();

[ Here I used List because it's considered a good practice to program to interfaces. ]

谁的年少不轻狂 2024-09-24 16:30:33
ArrayList<String[]> action = new ArrayList<String[]>();

不需要String[2]

ArrayList<String[]> action = new ArrayList<String[]>();

Don't need String[2];

王权女流氓 2024-09-24 16:30:33

正如已经回答的那样,您可以创建一个字符串数组的 ArrayList,如 @Péter Török 所写;

    //Declaration of an ArrayList of String Arrays
    ArrayList<String[]> listOfArrayList = new ArrayList<String[]>();

当将不同的字符串数组分配给此ArrayList时,每个字符串数组的长度将不同。

在下面的示例中,添加了 4 个不同的字符串数组,它们的长度各不相同。

String Array #1: len: 3
String Array #2: len: 1
String Array #3: len: 4
String Array #4: len: 2

演示代码如下;

import java.util.ArrayList;

public class TestMultiArray {

    public static void main(String[] args) {
        //Declaration of an ArrayList of String Arrays
        ArrayList<String[]> listOfArrayList = new ArrayList<String[]>();

        //Assignment of 4 different String Arrays with different lengths
        listOfArrayList.add( new String[]{"line1: test String 1","line1: test String 2","line1: test String 3"}  );
        listOfArrayList.add( new String[]{"line2: test String 1"}  );
        listOfArrayList.add( new String[]{"line3: test String 1","line3: test String 2","line3: test String 3", "line3: test String 4"}  );
        listOfArrayList.add( new String[]{"line4: test String 1","line4: test String 2"}  );

        // Printing out the ArrayList Contents of String Arrays
        // '

输出如下;

 $ line1: test String 1 $ line1: test String 2 $ line1: test String 3
 $ line2: test String 1
 $ line3: test String 1 $ line3: test String 2 $ line3: test String 3 $ line3: test String 4
 $ line4: test String 1 $ line4: test String 2

另请注意,您可以初始化一个新的 Sting 数组,如下所示;

new String[]{ str1, str2, str3,... }; // Assuming str's are String objects

所以这与以下相同;

String[] newStringArray = { str1, str2, str3 }; // Assuming str's are String objects

我编写此演示只是为了表明没有 ArrayList 对象,所有元素都是对字符串数组的不同实例的引用,因此每个字符串数组的长度不必相同,也不必相同这很重要。

最后一点:最佳实践是在 List 接口中使用 ArrayList,而不是您在问题中使用的 ArrayList。

最好使用List接口,如下所示;

    //Declaration of an ArrayList of String Arrays
    List<String[]> listOfArrayList = new ArrayList<String[]>();
is used to indicate the String elements of String Arrays for( int i = 0; i < listOfArrayList.size(); i++ ) { for( int j = 0; j < listOfArrayList.get(i).length; j++ ) System.out.printf(" $ " + listOfArrayList.get(i)[j]); System.out.println(); } } }

输出如下;

另请注意,您可以初始化一个新的 Sting 数组,如下所示;

所以这与以下相同;

我编写此演示只是为了表明没有 ArrayList 对象,所有元素都是对字符串数组的不同实例的引用,因此每个字符串数组的长度不必相同,也不必相同这很重要。

最后一点:最佳实践是在 List 接口中使用 ArrayList,而不是您在问题中使用的 ArrayList。

最好使用List接口,如下所示;

As already answered, you can create an ArrayList of String Arrays as @Péter Török written;

    //Declaration of an ArrayList of String Arrays
    ArrayList<String[]> listOfArrayList = new ArrayList<String[]>();

When assigning different String Arrays to this ArrayList, each String Array's length will be different.

In the following example, 4 different Array of String added, their lengths are varying.

String Array #1: len: 3
String Array #2: len: 1
String Array #3: len: 4
String Array #4: len: 2

The Demonstration code is as below;

import java.util.ArrayList;

public class TestMultiArray {

    public static void main(String[] args) {
        //Declaration of an ArrayList of String Arrays
        ArrayList<String[]> listOfArrayList = new ArrayList<String[]>();

        //Assignment of 4 different String Arrays with different lengths
        listOfArrayList.add( new String[]{"line1: test String 1","line1: test String 2","line1: test String 3"}  );
        listOfArrayList.add( new String[]{"line2: test String 1"}  );
        listOfArrayList.add( new String[]{"line3: test String 1","line3: test String 2","line3: test String 3", "line3: test String 4"}  );
        listOfArrayList.add( new String[]{"line4: test String 1","line4: test String 2"}  );

        // Printing out the ArrayList Contents of String Arrays
        // '

And the output is as follows;

 $ line1: test String 1 $ line1: test String 2 $ line1: test String 3
 $ line2: test String 1
 $ line3: test String 1 $ line3: test String 2 $ line3: test String 3 $ line3: test String 4
 $ line4: test String 1 $ line4: test String 2

Also notify that you can initialize a new Array of Sting as below;

new String[]{ str1, str2, str3,... }; // Assuming str's are String objects

So this is same with;

String[] newStringArray = { str1, str2, str3 }; // Assuming str's are String objects

I've written this demonstration just to show that no theArrayList object, all the elements are references to different instantiations of String Arrays, thus the length of each String Arrays are not have to be the same, neither it is important.

One last note: It will be best practice to use the ArrayList within a List interface, instead of which that you've used in your question.

It will be better to use the List interface as below;

    //Declaration of an ArrayList of String Arrays
    List<String[]> listOfArrayList = new ArrayList<String[]>();
is used to indicate the String elements of String Arrays for( int i = 0; i < listOfArrayList.size(); i++ ) { for( int j = 0; j < listOfArrayList.get(i).length; j++ ) System.out.printf(" $ " + listOfArrayList.get(i)[j]); System.out.println(); } } }

And the output is as follows;

Also notify that you can initialize a new Array of Sting as below;

So this is same with;

I've written this demonstration just to show that no theArrayList object, all the elements are references to different instantiations of String Arrays, thus the length of each String Arrays are not have to be the same, neither it is important.

One last note: It will be best practice to use the ArrayList within a List interface, instead of which that you've used in your question.

It will be better to use the List interface as below;

轮廓§ 2024-09-24 16:30:33

这非常有效。

ArrayList<String[]> a = new ArrayList<String[]>();
    a.add(new String[3]);
    a.get(0)[0] = "Zubair";
    a.get(0)[1] = "Borkala";
    a.get(0)[2] = "Kerala";
System.out.println(a.get(0)[1]);

结果将是

Borkala

This works very well.

ArrayList<String[]> a = new ArrayList<String[]>();
    a.add(new String[3]);
    a.get(0)[0] = "Zubair";
    a.get(0)[1] = "Borkala";
    a.get(0)[2] = "Kerala";
System.out.println(a.get(0)[1]);

Result will be

Borkala
屋檐 2024-09-24 16:30:33
  1. ArrayList操作一样创建ArrayList。

    在 JDK 1.5 或更高版本中使用 ArrayList 引用名称。

    在 JDK 1.4 或更低版本中,使用ArrayList引用名称。

  2. 指定访问说明符:

    • 公开,可以在任何地方访问
    • 私有,在类内访问
    • 受保护,可在类和不同的包子类中访问
  3. 然后指定它将被分配的引用

    action = new ArrayList();
    
  4. JVM 中分配的引用 new 关键字将在运行时为该对象分配内存。

    您不应该在声明的地方分配值,因为您要求没有固定大小。

  5. 最后你可以使用ArrayList中的add()方法。使用类似

    action.add(新字符串[你需要多少])
    

    它将在堆中分配特定的内存区域。

  1. Create the ArrayList like ArrayList action.

    In JDK 1.5 or higher use ArrayList <string[]> reference name.

    In JDK 1.4 or lower use ArrayList reference name.

  2. Specify the access specifiers:

    • public, can be accessed anywhere
    • private, accessed within the class
    • protected, accessed within the class and different package subclasses
  3. Then specify the reference it will be assigned in

    action = new ArrayList<String[]>();
    
  4. In JVM new keyword will allocate memory in runtime for the object.

    You should not assigned the value where declared, because you are asking without fixed size.

  5. Finally you can be use the add() method in ArrayList. Use like

    action.add(new string[how much you need])
    

    It will allocate the specific memory area in heap.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文