Java - 多维数组的数组列表或多维数组列表的数组列表?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19337788/
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-12 16:14:47  来源:igfitidea点击:

Java - arraylist of multidimensional arrays or arraylist of multidimensional arraylists?

javaarraysmultidimensional-arrayarraylist

提问by user797963

I'm learning Java and am trying to wrap my head around these data structures. I'm coming from Python/PHP so I'm used to dynamically sizing arrays on the fly and being able to store multiple data types in one array.

我正在学习 Java 并试图围绕这些数据结构进行思考。我来自 Python/PHP,所以我习惯于动态调整数组大小并能够在一个数组中存储多种数据类型。

How would I best store this data? Say I don't know how many rows I'll have, but I do know that each row will hold 2 columns of data. One column being a string, and the other column being a double.

我将如何最好地存储这些数据?假设我不知道有多少行,但我知道每行将包含 2 列数据。一列是字符串,另一列是双精度。

Example in pseudo-ish code if I had 3 rows:

如果我有 3 行,则在伪代码中的示例:

array(array("description1", 10.00),
      array("description2", 12.00),
      array("description3", 14.00));

Then, I want to loop through the array to process the datawith something like:

然后,我想遍历数组来处理数据,例如:

foreach(rows as row){
    myStringVal = row[0]; //on first iteration would be "description1"
    myIntVal = row[1];    //on first iteration would be 10.00
    ... do something with the values ...
}

I'm thinking I need to create an arraylist that holds an array, but I can't store both strings and doubles in a java array, so what do i do? Do I use a map instead, and treat it as if it were an array? For example, do I create a map where the first element is a numeric ID for each row, the second element is the string value, the 3rd element is the double value, and then I use a loop to increase a counter and grab each row from the map that using the numeric ID>

我想我需要创建一个包含数组的数组列表,但我不能在 java 数组中同时存储字符串和双精度值,那我该怎么办?我是否使用地图,并将其视为数组?例如,我是否创建了一个地图,其中第一个元素是每一行的数字 ID,第二个元素是字符串值,第三个元素是双精度值,然后我使用循环增加一个计数器并抓取每一行从使用数字 ID 的地图>

Really confused as to how this will work. Any suggestions? Thanks!

真的很困惑这将如何工作。有什么建议?谢谢!

采纳答案by Mike 'Pomax' Kamermans

You're not storing "different kind of values", you're storing semantic data that encodes "a tuple" but using the non-java concept of just sticking that in an array. Don't do that, instead use the C/C++/Java concept of encoding linked data as a struct/object:

您不是在存储“不同类型的值”,而是在存储对“元组”进行编码的语义数据,而是使用将其粘贴在数组中的非 Java 概念。不要这样做,而是使用将链接数据编码为结构/对象的 C/C++/Java 概念:

// our "struct"esque class for the linked data we're handling
class MyObject {
  String description;
  float value;
  Public MyObject(String description, float value) {
    this.description = description;
    this.value = value;
  }
}

// build our list of objects
ArrayList<MyObject> mylist = new ArrayList<MyObject>();
mylist.add(new MyObject("description1", 10));
mylist.add(new MyObject("description2", 12));
mylist.add(new MyObject("description3", 14));
...

// and then we iterate
String d; float v;
for(MyObject m: mylist) {
  d = m.description;
  v = m.value;
  ...
}

回答by TofuBeer

Use a class instead of an array.

使用类而不是数组。

public class X
{
    private final String description;
    private final double value;

    public X(final String desc,
             final double val)
    {
        decription = desc;
        value      = val;
    }

    // getters
}

If you want to be able to change the description and value then don't make them final.

如果您希望能够更改描述和值,请不要将它们设为最终版本。

You could make the descriptionand valuevariables public, but I would avoid that temptation.

你可以公开descriptionvalue变量,但我会避免这种诱惑。

回答by Makoto

It lookslike a List<Map<String, BigDecimal>>to me.

看起来List<Map<String, BigDecimal>>我。

  • The Listwill give you the ability to enter more than one element continuously.
  • The Mapwill give you the association between the Stringand floating-point value, which I've chosen BigDecimalto represent. You could use Doubleif you wanted.

    List<Map<String, BigDecimal>> elements = new ArrayList<>();
    elements.add(new HashMap<String, BigDecimal>());
    elements.get(0).put("description1", BigDecimal.valueOf("10.00"));
    
  • List将使您能够连续输入多个元素。
  • Map会给你之间的关联String和浮点值,我选择BigDecimal来表示。Double如果你愿意,你可以使用。

    List<Map<String, BigDecimal>> elements = new ArrayList<>();
    elements.add(new HashMap<String, BigDecimal>());
    elements.get(0).put("description1", BigDecimal.valueOf("10.00"));