aspose file tools*
The moose likes Beginning Java and the fly likes How do I convert a list of list into array [] [] Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "How do I convert a list of list into array [] []" Watch "How do I convert a list of list into array [] []" New topic
Author

How do I convert a list of list into array [] []

Sege Stephen
Ranch Hand

Joined: Oct 06, 2007
Posts: 51
I have a List

List<List> myList has

[[UK, 1, 10.13.64.1, A, CACHE], [UK, 1, 10.13.64.2, B, CACHE], [UK, 1, 10.13.64.3, C, CACHE]]

stored in it, I need to put this data in a Table. so for the row and column it will be

A B C D E
1 UK, 1, 10.13.64.1, A, CACHE
2 UK, 1, 10.13.64.2, B, CACHE
3 UK, 1, 10.13.64.3, C, CACHE

How do i do this? I reckon I need to use a nexted for loop, but I am not getting the desired result.

how do I convert to it Object [][] data
Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

Sege Stephen wrote: I reckon I need to use a nexted for loop, but I am not getting the desired result.

your guess is right! by the way : what problem you are facing?

Sege Stephen wrote: how do I convert to it Object [][] data

do you mean converting List of List into Array of Arrays? you need to loop the outer List and the convert to Array and add this array element into another array. hence Array of Arrays. If you only need insertion of List element/List, then this operation/looping is unnecessary.

hope this helps
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38363
    
  23
You don't have an array of arrays in that List. You have an array of objects. Why don't you create an object which can return
[UK, 1, 10.13.64.2, B, CACHE]
from its toString method, then you only need one array. You can then use a List<Foo> rather than a List<List>.

And as previously stated, it helps us little to say "not getting the desired result".
Sege Stephen
Ranch Hand

Joined: Oct 06, 2007
Posts: 51
Thanks for the feedback so far






This is not working
Wouter Oet
Saloon Keeper

Joined: Oct 25, 2008
Posts: 2700

In every iteration you're creating a new LinkedList and discarding the older one. You might want to add it to a "master" LinkedList.


"Any fool can write code that a computer can understand. Good programmers write code that humans can understand." --- Martin Fowler
Please correct my English.
Sege Stephen
Ranch Hand

Joined: Oct 06, 2007
Posts: 51
Following the response that I should make it an Array of object,

i changed my code to

Object[][] data;




The System out println is printing the desired result

[UK, 1, 1.13.64.2.91, 1, CACHE, false]was and is
[UK, 1, 1.13.64.92, 1, CACHE, false]was and is
[UK, 1, 1.13.64.93, 1, CACHE, false]was and is

but how do I set this into my array of object ? ie i need this in a table .
Please help, I am almost there

I want it to be like Object[][] data = {{"1", "2" , "3", "4" ,"5", "4"}, {"a", "b" , "c", "d" ,"e", "f"}};
Baichuan Huang
Greenhorn

Joined: Oct 04, 2010
Posts: 19
Daniel Sagayaraj
Ranch Hand

Joined: Mar 15, 2010
Posts: 32
import java.util.ArrayList;
import java.util.List;

import org.apache.log4j.Logger;

public class temp {

public static void main(String[] args) {

Logger logger = Logger.getLogger(temp.class);

// Creating the List One

List<String> fruit = new ArrayList<String>();

fruit.add("Apple");
fruit.add("Mango");
fruit.add("Orange");
fruit.add("Grapes");

// Creating the List Two
List<String> flower = new ArrayList<String>();
flower.add("Rose");
flower.add("Lilly");

// Creating the List of List....

List<List<String>> finalList = new ArrayList<List<String>>();

finalList.add(fruit);
finalList.add(flower);
// Small calculation for deciding the size of the array

int max = 0;
if (fruit.size() > flower.size()) {
max = fruit.size();
} else {
max = flower.size();
}

// Array[][] is Created

Object arrays[][] = new Object[finalList.size()][max];

// Array[][] is loaded with the values

for (int i = 0; i < finalList.size(); i++) {

arrays[i] = finalList.get(i).toArray();

}

// Array[][] is printed..

for (int i = 0; i < finalList.size(); i++) {

for (int j = 0; j < max; j++) {
try {
logger.info(arrays[i][j]);
} catch (ArrayIndexOutOfBoundsException e) {
break;// To handle if the list is not in equal size
}

}
}

}

}


-------------------------
Output::


2010-10-05 17:32:02 temp [INFO] Apple
2010-10-05 17:32:02 temp [INFO] Mango
2010-10-05 17:32:02 temp [INFO] Orange
2010-10-05 17:32:02 temp [INFO] Grapes
2010-10-05 17:32:02 temp [INFO] Rose
2010-10-05 17:32:02 temp [INFO] Lilly

Kevin Workman
Ranch Hand

Joined: Sep 28, 2010
Posts: 151
Sege Stephen wrote:Following the response that I should make it an Array of object,

i changed my code to

Object[][] data;


I think you're missing the point. Right now, you have the contained Lists holding data that almost definitely should be held in Objects representing whatever that data is. Say I had a List of animals, each with a name, an age, and a sound they make. Your way would be doing something like:



Then, to access a particular attribute, you'd have to remember which index of the contained list represented that data. That's not the way to do it. Instead, create another class that contains the data. We say create a List of Objects because every class you write will extend Object. We don't mean literally use references to instances of Objects. So my example would be:



Now, to get a particular attibute, you would call the getter function of whatever data you're looking for. Or, you could put it all in a toString() method as mentioned above. That just depends on your requirements.

PS- It Doesn't Work Is Useless
 
Don't get me started about those stupid light bulbs.
 
subject: How do I convert a list of list into array [] []