save get values using arrayList java
In my example, I try to get val(0) and val(1) each time.
Afterfor
loop I need to save my values to use them for other calculations:
String[] columns = { "col1" ,开发者_JAVA百科 "col2" };
String[] y = { "TEST", "BUG" ,"ENH" };
List<int[]> values = new ArrayList<int[]>();
for (int j = 0; j < y.length; j++) {
// do some actions
for(int[] v : values) {
//v is the array for one iteration, use it like this:
int col1 = v[0];
int col2 = v[1];
values .add(v);
}
}
System.out.prinln(values) =>gives : []
Out of for
loop, my values are raised, how do can I do to get my values after for
?
I don't understand the question either, but, here's my best guess at interpreting what the question is trying to do:
String[] columns = {"col1" , "col2"};
String[] y = { "TEST", "BUG" ,"ENH"};
int[][] values = new int[y.length][columns.length]; // 2D array
for (int j = 0; j < y.length; j++) {
for (k = 0; k < columns.length; k++) {
values[j][k] = table.getVal(j, columns[k]);
}
}
for (int value : values) {
// do something with value
}
Without knowing the structure of 'table', your data in the int array 'values' should be readily accessible via a for loop:
for (int v: value) {
//use 'v' here
}
or directly:
values[0];
values[1];
...etc
However, it might simply be a language barrier here preventing us from understanding the true problem.
If you want to get the values of all iterations after the loop, you first have to provide a fitting data structure. An easy way might be to use a 2-dimensional array:
int[][] values = new int[y.length][columns.length];
However, I'd recommend using a collection instead (assuming the original values
has to be an array):
List<int[]> values = new ArrayList<int[]>();
Then fill that in your loop, either using array index y
or just adding a new array in each iteration.
After the loop you can then iterate over the result, like this:
for(int[] v : values) {
//v is the array for one iteration, use it like this:
int col1 = v[0];
int col2 = v[1];
//or like this
for(int value : v) {
//value is the value of one column in one iteration
}
}
UPDATE:
In your updated code you're not putting anything into the list.
Note that you still have to create an array per iteration, get the values from your source (IIRC it was named table
) and put them into the array, then add the array to the list using values.add(...)
.
Also note that the loop I added above is meant to read the values later, it doesn't show you how to fill the list in the first place.
精彩评论