convert multiarray to arraylist
Hi all!
Can anyone please advise me on how should i convert a multiarray to an arraylist. each array in the multiarray is of various lengths.i cannot create arraylist manually because the number of ti as in the code might varies.
The existing code goes like this:
public static void terminalComp(String []cn, int ti) {//
for (int i = 0 ; i < cn.length ; i++) {
terminalComp[ti]=cn;
}
}
this method is called by some other method to pass splitted value read from a file.
Thank you in advance for your help and time.
[595 byte] By [
brisJavaa] at [2007-11-27 8:27:33]

# 1
I fail to see any multiarray in the code provided, but here's an example.
int[][] multi = new int[][] {{1, 2}, {1, 2, 3}, {1, 2, 3, 4}};
List<List<Integer>> multiList = new ArrayList<List<Integer>>(multi.length);
for (int[] array : multi) {
List<Integer> list = new ArrayList<Integer>(array.length);
for (int i : array) {
list.add(i);
}
multiList.add(list);
}
dwga at 2007-7-12 20:17:14 >

# 2
err, static void List<T> Arrays.asList(T[]) does it for you.
ejpa at 2007-7-12 20:17:14 >

# 3
> err, static void List<T> Arrays.asList(T[]) does it> for you.err, depends on the array, if it is an array of primitives e.g. Arrays.asList(new int[] {1,2,3}) will return a List<int[]>.
dwga at 2007-7-12 20:17:14 >

# 4
thank you for your help!! the code really works out for me..
# 5
Hi again..
My code now goes like this:
for (int i = 0 ; i < cn.length ; i++) {
terminalComp[ti]=cn;
terminalcompsize[ti]=cn.length;
}
for (String[] array : terminalComp) {
List<String> list = new ArrayList<String>();
for (String i : array) {
list.add(i);
}
multiList.add(list);//multiList.get(2).get(1));==multiList.get(row=elementIndex).get(col=componentIndex));
}
I've tried to check the value for every single element in the list. But now I am wondering if there is any direct way for me so that i don't have to use the double array terminalComp[ti] or copy every item from terminalComp. This is because, i have initialized the terminalComp array to be of size [100][100]. So if I copy every items from the array it will also copy the unoccupied (actually containing null) cell into the new MultiList. ti actually stores the number of elements existed in the file, i stores the number of components for each elements.
For your information, I have created ArrayList for storing the elements.
Real big thank you to all help given.
# 6
I don't quite understand your question.
If you want to print out the contents of the listSystem.out.println(multiList);
if you want to iterate over everything in the listfor (List<Integer> list : multiList) {
for (Integer i : list) {
// do something with i
}
}
dwga at 2007-7-12 20:17:14 >

# 7
Opss, sorry..hope i make myself clearer this time..
i seek some advice if anyone could give on how to store a value as in a multi array.. if u see from my code previously, i have a multiarray named terminalComp[ti]. This array actually stores a value of type string (represented by cn for component names)..i would protray the table to be 2-dimension, where the columns (represented by ) store the component index and the row (represented by [ti]) stores elements index..
i appreciate the help previously on how i could copy all the values from the existing terminalComp[ti].. it really worked and i like it.. the problem is that if i copy straight from it, it will also copy the unsigned value (null) that is in the terminalComp array. this is because some of the columns in the array is not filled, because the element size varies..
could anyone please give me some examples or tell their experience how they sort out things like this..
TQ in advance for the time and help..
# 8
When copying values from terminalComp check whether the value is null before copying and don't copy if it is.
Something likefor ... {
if (terminalComp[ti] != null) {
// copy the value to the list
}
}
dwga at 2007-7-12 20:17:14 >

# 9
Does this have anything to do with the OP's problem? For a start, he is copying values to terminalComp, not from it.
ejpa at 2007-7-12 20:17:15 >

# 10
> Does this have anything to do with the OP's problem?
> For a start, he is copying values to
> terminalComp, not from it.
If that's the case he's certainly making things complicated by saying things like "how i could copy all the values from the existing terminalComp[ti]".
dwga at 2007-7-12 20:17:15 >

# 11
hi all,
I don't think this is OP problem. I managed to copy the array values into the arraylist and filter out the 'null'. but what i am looking for right now is some method that would enable me to create/update the arraylist which represent the double array straight, just as how i had update the double array.
seems like this is impossible anyway. i tried ArrayList<ArrayList><ArrayList><String>>> but gor ridiculous value. it is totally away from what i planned to do earlier.
thanx for you guys concern, anyway..
# 12
i have recently post a question to the algorithm forum (about crossover function) but yet to get any help. for those of you interested please visit http://forum.java.sun.com/thread.jspa?threadID=5188821&tstart=0 thank you in advance.Message was edited by: brisJava
# 13
> the 'null'. but what i am looking for right now is> some method that would enable me to create/update the> arraylist which represent the double array straight,> just as how i had update the double array.I don't understand, please elaborate.
dwga at 2007-7-12 20:17:15 >

# 14
just like when we add some value into a single arrayList, we would go varName.add(somevalue).
since i want to store a value in an arrayList which the arrayList is can store something like a 2-dimension array. so the result would be something like
{{component1ofelement1, component2ofelement1},
{component1ofelement2, component2ofelement2, component3ofelement2}, {component1ofelement13, component2ofelement3}};