List Arrary Double[] entering values
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
myarray[row][column] = value;
so how do I do this with the List?
mylist.add --- something?
List<double []> // creates a list that stores array开发者_StackOverflow社区s 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);
You should avoid using arrays as generic type parameters as you can easily end up with "unchecked" warnings. Use collections (typically ArrayList
) instead.
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.)
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
.)
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}
};
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
List<T> myList = new ArrayList<T>();
T should be same. If T is double[] then it should be new ArrayList<double[]>
()
then get(index)
which is double array and get(index)[j]
which is value. same logic with
d[i][j] = get(index)[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]);
精彩评论