java 列出 Array Double[] 输入值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4619306/
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
List Arrary Double[] entering values
提问by Mark Worsnop
Using the examples below the 2nd part I see how to add values to a single dim array. But the 2 dim array I dont yet understand. In the 1st example the 1,2,3 I need to get from a database. The DB part I have figured out but how to put in the values I dont know. If I was using an array it would be
使用第二部分下面的示例,我了解了如何将值添加到单个昏暗数组中。但是我还不明白的 2 暗阵列。在第一个示例中,我需要从数据库中获取 1,2,3。我已经弄清楚了 DB 部分,但如何输入我不知道的值。如果我使用的是数组,它将是
myarray[row][column] = value;
so how do I do this with the List?
那么我该如何使用 List 做到这一点呢?
mylist.add --- something?
mylist.add --- 什么?
List<double []> // creates a list that stores arrays of doubles.
List<double []> myList = new ArrayList<Double>();
myList.add(new double [] {1,2,3});
myList.add(new double [] {4,5,6});
List<Double> myList = new ArrayList<Double>();
myList.add(1);
myList.add(2);
myList.add(3);
回答by
List<T> myList = new ArrayList<T>();
T should be same. If T is double[] then it should be new ArrayList<double[]>
()
T 应该是一样的。如果 T 是 double[] 那么它应该是new ArrayList<double[]>
()
then get(index)
which is double array and get(index)[j]
which is value. same logic with
那么get(index)
哪个是双数组,get(index)[j]
哪个是值。相同的逻辑
d[i][j] = get(index)[j]
d[i][j] = 获取(索引)[j]
List<double []> myList = new ArrayList<double[]>();
myList.add(new double [] {1,2,3});
myList.add(new double [] {4,5,6});
System.out.println(myList.get(0)[1]);
回答by Kevin Coppock
If you're talking about just doing inline initialization of a multidimensional array, I'm pretty sure you just use subsetted braces, like so:
如果您只是在谈论对多维数组进行内联初始化,我很确定您只是使用子集大括号,如下所示:
Double array2D[][] = {
{1, 2, 3},
{4, 5, 6},
{7, 8, 9}
};
回答by Mike Yockey
Do you want a collection-based solution for two-dimensional arrays? If so, then you're looking for a list of lists.
您想要二维数组的基于集合的解决方案吗?如果是这样,那么您正在寻找列表列表。
//Declaration
List<List<double[]>> twoDimList = new ArrayList<List<double>>();
//Adding values
twoDimList.add(new ArrayList<double>(1));
twoDimList.add(new ArrayList<double>(2));
twoDimList.add(new ArrayList<double>(3));
//Retrieving values
double value = twoDimList.get(1).get(1); //returns 1
回答by Laurence Gonsalves
You should avoid using arrays as generic type parameters as you can easily end up with "unchecked" warnings. Use collections (typically ArrayList
) instead.
您应该避免使用数组作为泛型类型参数,因为您很容易以“未检查”警告结束。使用集合(通常ArrayList
)代替。
So instead of this:
所以而不是这个:
List<double []> // creates a list that stores arrays of doubles.
List<double []> myList = new ArrayList<Double>();
myList.add(new double [] {1,2,3});
myList.add(new double [] {4,5,6});
Do something like this:
做这样的事情:
List<List<Double>> myList = new ArrayList<List<Double>>();
myList.add(new ArrayList<Double>());
myList.add(new ArrayList<Double>());
myList.get(0).add(1);
myList.get(0).add(2);
myList.get(0).add(3);
Or to initialize together, you'd probably do something like:
或者要一起初始化,您可能会执行以下操作:
List<List<Double>> myList = new ArrayList<List<Double>>();
List<Double> nested;
nested = new ArrayList<Double>();
nested.add(1);
nested.add(2);
nested.add(3);
myList.add(nested);
nested = new ArrayList<Double>();
nested.add(4);
nested.add(5);
nested.add(6);
myList.add(nested);
(You can add nested
to myList
before or after adding the elements to nested
-- either way works so do whatever is clearer to you.)
(您可以在将元素添加nested
到myList
之前或之后添加到nested
- 无论哪种方式都有效,所以做任何对您来说更清楚的事情。)
To retrieve a specific element:
要检索特定元素:
double value = myList.get(x).get(y);
To set a specific element:
要设置特定元素:
myList.get(x).set(y, value);
This assumes that the list are already the right size, and that you just want to change the value of an existing element. (In other words, just like set
behaves on a simple List
.)
这假设列表的大小已经合适,并且您只想更改现有元素的值。(换句话说,就像set
在简单的List
.上的行为一样。)