Java 字符串数组的ArrayList

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1921181/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 01:26:54  来源:igfitidea点击:

ArrayList of String Arrays

javaarraysarraylist

提问by Johan

I would like to do something like this:

我想做这样的事情:

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

This does not seem to work. Whats the easiest way of storing multiple addresses with 3 fields per address in an array without creating a separate class for it?

这似乎不起作用。在数组中存储每个地址有 3 个字段的多个地址而不为其创建单独的类的最简单方法是什么?

采纳答案by Grundlefleck

Use a second ArrayList for the 3 strings, not a primitive array. Ie.
private List<List<String>> addresses = new ArrayList<List<String>>();

对 3 个字符串使用第二个 ArrayList,而不是原始数组。IE。
private List<List<String>> addresses = new ArrayList<List<String>>();

Then you can have:

然后你可以有:

ArrayList<String> singleAddress = new ArrayList<String>();
singleAddress.add("17 Fake Street");
singleAddress.add("Phoney town");
singleAddress.add("Makebelieveland");

addresses.add(singleAddress);

(I think some strange things can happen with type erasure here, but I don't think it should matter here)

(我认为这里的类型擦除可能会发生一些奇怪的事情,但我认为这不重要)

If you're dead set on using a primitive array, only a minor change is required to get your example to work. As explained in other answers, the size of the array can not be included in the declaration. So changing:

如果您一味地使用原始数组,则只需稍作更改即可使您的示例工作。如其他答案中所述,数组的大小不能包含在声明中。所以改变:

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

to

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

will work.

将工作。

回答by cletus

I wouldn't use arrays. They're problematic for several reasons and you can't declare it in terms of a specific array size anyway. Try:

我不会使用数组。由于多种原因,它们存在问题,无论如何您都无法根据特定的数组大小来声明它。尝试:

List<List<String>> addresses = new ArrayList<List<String>>();

But honestly for addresses, I'd create a class to model them.

但老实说,对于地址,我会创建一个类来为它们建模。

If you were to use arrays it would be:

如果您要使用数组,它将是:

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

ie you can't declare the size of the array.

即你不能声明数组的大小。

Lastly, don't declare your types as concrete types in instances like this (ie for addresses). Use the interface as I've done above. This applies to member variables, return types and parameter types.

最后,不要在这样的实例(即 for addresses)中将您的类型声明为具体类型。像我上面所做的那样使用界面。这适用于成员变量、返回类型和参数类型。

回答by bruno conde

You can't force the String arrays to have a specific size. You can do this:

您不能强制 String 数组具有特定大小。你可以这样做:

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

but an array of any size can be added to this list.

但是可以将任意大小的数组添加到此列表中。

However, as others have mentioned, the correct thing to do here is to create a separate class representing addresses. Then you would have something like:

然而,正如其他人所提到的,这里正确的做法是创建一个单独的类来表示地址。然后你会有类似的东西:

private List<Address> addresses = new ArrayList<Address>();

回答by Upul Bandara

List<String[]> addresses = new ArrayList<String[]>();
String[] addressesArr  = new String[3];

addressesArr[0] = "zero";
addressesArr[1] = "one";
addressesArr[2] = "two";

addresses.add(addressesArr);

回答by shaFayi

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

this will work defenitely...

这肯定会起作用......

回答by A.G.THAMAYS

try this

尝试这个

ArrayList<ArrayList<String>> PriceModelList = new ArrayList<>();
    ArrayList<ArrayList<String>> PriceQtyList = new ArrayList<>();
    ArrayList<ArrayList<String>> PriceTotalList = new ArrayList<>();

    for (int i = 0; i < CustomerNames.length; i++) {
        PriceModelList.add(new ArrayList<String>());
        String[] PriceModel = {"s6", "s7", "note4", "note5", "j5", "j6"};
        for (int j = 0; j < PriceModel.length; j++) {
            PriceModelList.get(i).add(PriceModel[j]);
        }

        PriceQtyList.add(new ArrayList<String>());
        String[] PriceQut = {"12", "13", "21", "15", "43", "21"};
        for (int k = 0; k < PriceQut.length; k++) {
            PriceQtyList.get(i).add(PriceQut[k]);
        }

        PriceTotalList.add(new ArrayList<String>());
        String[] PriceTotal = {"1323", "1312321", "43123212", "43434", "12312", "43322"};
        for (int m = 0; m < PriceTotal.length; m++) {
            PriceTotalList.get(i).add(PriceTotal[m]);
        }
    }

    ArrayList<ArrayList<ArrayList<String>>> CustomersShoppingLists = new ArrayList<>();
    CustomersShoppingLists.add(PriceModelList);
    CustomersShoppingLists.add(PriceQtyList);
    CustomersShoppingLists.add(PriceTotalList);

回答by Yasir Shabbir Choudhary

Simple and straight forward way to create ArrayList of String

创建 String ArrayList 的简单直接的方法

    List<String> category = Arrays.asList("everton", "liverpool", "swansea", "chelsea");

Cheers

干杯

回答by Harsimranjit Singh Kler

Following works in Java 8..

以下在 Java 8 中工作..

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