如何创建动态二维数组 [JAVA]

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

How to create Dynamic Two Dimensional Array [JAVA]

javalistmultidimensional-array

提问by Wearybands

I am new to java and I want to create a 2-dimensional array in which the rows are static but the columns are dynamic.

我是 Java 新手,我想创建一个二维数组,其中行是静态的,但列是动态的。

double [][] lastSecArray = new double [2][41];
int lastValue = -1;
public foo (){
   lastValue++;
   lastSecArray[0][lastValue] = st_ms;
   //more code here
}

The value of lastValue increases and when it reaches 41 my program gives me Array Index out of bound. Which is what I should expect.

lastValue 的值增加,当它达到 41 时,我的程序给了我超出范围的数组索引。这是我应该期待的。

How can I make the column of teh array dynamic so that no matter how large the value of lastValue increases it runs.

如何使数组的列动态化,以便无论 lastValue 的值增加多少,它都会运行。

采纳答案by Kevin Bowersox

It may be more appropriate to use a Map<Double, List<Double>>. Having the value of the Mapas a List<Double>will allow you to expand the list as opposed to an array which has a fixed size.

使用Map<Double, List<Double>>. 拥有Mapas a的值List<Double>将允许您扩展列表而不是具有固定大小的数组。

public static void main(String[] args) throws CloneNotSupportedException {
    Map<Double, List<Double>> myMap = create(1, 3);
}

public static Map<Double, List<Double>> create(double row, double column) {
    Map<Double, List<Double>> doubleMap = new HashMap<Double, List<Double>>();

    for (double x = 0; x < row; x++) {
        for (double y = 0; y < column; y++) {
            doubleMap.put(x, new ArrayList<Double>());
        }
    }
    return doubleMap;
}

回答by BobTheBuilder

Use ArrayListinstead.

请改用ArrayList

You can use:

您可以使用:

List<List<Double>> = new ArrayList<>();

ArrayListis dynamically-extendable. You can create ArrayListboth for rows and cols.

ArrayList是动态可扩展的。您可以ArrayList为行和列创建。

回答by pulasthi

in Java arrays are fixed size so what you are saying is not possoble.

Java 数组中的大小是固定的,因此您所说的不可能。

take a look at this thread might help you.

看看这个线程可能对你有帮助。

Variable length (Dynamic) Arrays in Java

Java 中的可变长度(动态)数组

回答by Suresh Atta

If you know the element size at runtime

如果您知道运行时的元素大小

int size=runtime decides;
double [][] lastSecArray = new double [2][size];

回答by Batyr Malik

Try to use

尝试使用

Map<String, ArrayList> lastSecArray = new HashMap<>();
    ArrayList value = new ArrayList();
    value.add(0);
    value.add(1);
    value.add(2);
    lastSecArray.put("0", value);

so you can operate with

所以你可以操作

lastSecArray.size()

or

或者

lastSecArray.put(...)

or array in array

或数组中的数组

回答by abletterer

I assume you're coming from the C-world. In Java, you have many objects which represent arrays.

我假设你来自 C 世界。在 Java 中,您有许多表示数组的对象。

I suggest you to check this link : ArrayList. This is a class which uses a resizable array.

我建议您查看此链接:ArrayList。这是一个使用可调整大小数组的类。

I think this is a good way to have a dynamic two dimensionnal Array in Java.

我认为这是在 Java 中拥有动态二维数组的好方法。

As ArrayList is a template class, you are able to create ArrayList<ArrayList<double>>, or if you want to have a "static" number of rows, you can create ArrayList<double[2]>.

由于 ArrayList 是一个模板类,您可以创建ArrayList<ArrayList<double>>,或者如果您想要“静态”行数,您可以创建ArrayList<double[2]>.