6

In Java how do you convert a ArrayList into a two dimensional array Object[][]?

From comments: I will describe you the problem with more details: an XML file includes a list of contacts (e.g. name, address...). The only way I can obtain this information is through an ArrayList, which will be given to me. As I need to store the content of this array list in a Java Swing table in an ordered manner, I was thinking to convert it into a two dimensional array of objects

4
  • an array list is 1D, I don't understand what your question means Commented Jan 27, 2010 at 11:44
  • perhaps the ArrayList contains a List of Object arrays... Commented Jan 27, 2010 at 11:47
  • I will describe you the problem with more details: an XML file includes a list of contacts (e.g. name, address...). The only way I can obtain this information is through an ArrayList<Contacts>, which will be given to me. As I need to store the content of this array list in a table in an ordered manner, I was thinking to convert it into a two dimensional array of objects... Commented Jan 27, 2010 at 11:50
  • 2
    what kind of table? database table? swing JTable? html table? Commented Jan 27, 2010 at 12:00

8 Answers 8

8

I presume you are using the JTable(Object[][], Object[]) constructor.

Instead of converting an ArrayList<Contact> into an Object[][], try using the JTable(TableModel) constructor. You can write a custom class that implements the TableModel interface. Sun has already provided the AbstractTableModel class for you to extend to make your life a little easier.

public class ContactTableModel extends AbstractTableModel {

    private List<Contact> contacts;

    public ContactTableModel(List<Contact> contacts) {
        this.contacts = contacts;
    }

    public int getColumnCount() {
        // return however many columns you want
    }

    public int getRowCount() {
        return contacts.size();
    }

    public String getColumnName(int columnIndex) {
        switch (columnIndex) {
        case 0: return "Name";
        case 1: return "Age";
        case 2: return "Telephone";
        // ...
        }
    }

    public Object getValueAt(int rowIndex, int columnIndex) {
        Contact contact = contacts.get(rowIndex);

        switch (columnIndex) {
        case 0: return contact.getName();
        case 1: return contact.getAge();
        case 2: return contact.getTelephone();
        // ...
        }
    }

}

Later on...

List<Contact> contacts = ...;
TableModel tableModel = new ContactTableModel(contacts);
JTable table = new JTable(tableModel);
Sign up to request clarification or add additional context in comments.

Comments

2

The simple way is to add a method to the Contact like this:

public Object[] toObjectArray() {
    return new Object[] { getName(), getAddress, /* ... */ };
}

and use it like this:

ArrayList<Contact> contacts = /* ... */
Object[][] table = new Object[contacts.size()][];
for (int i = 0; i < contacts.size(); i++) {
    table[i] = contacts.get(i).toObjectArray();
}

Comments

2

Try this:

ArrayList<Integer> list = new ArrayList<Integer>();
list.add(..);
list.add(..);
list.add(..);
list.add(..);
list.add(..);
list.add(..);
int[][] a = new int[list.size()][list.size()];
    for(int i =0; i < list.size(); i++){
      for(int j =0; j <list.size(); j++){
        a[i][j]= list.get(j +( list.size() * i));
      }
  }

Comments

1

I managed to find "a way" to do so, knowing the number of attributes each contacts has (6). So considering an ArrayList listofContacts

    int numberOfContacts = listofContacts.size()/6;
    Object[][] newArrayContent = new Object[numberOfContacts][6];

    for(int x = 0; x<numberOfContacts; x++){
        for(int z = 0; z < 6; z++){
        int y = 6 * x;
        newArrayContent [x][z] = list.get(y+z); 
        System.out.println(newArrayContent [x][z].toString());
        }
    }

Comments

0

What you really want is to sort the ArrayList. To do that your Contacts class must implement a Comparator method.

Check the next page for an example: http://www.java-examples.com/sort-java-arraylist-descending-order-using-comparator-example

Comments

0

I will recommend that you parse your XML into java objects and store the object in a custom data object. This will make it easier for you to do many operations on the available data.

Here is small tutorial on how to do it.

Comments

0
public static String[][] convertListIntoArrayObj(List<TeamMenuSelected> possibilities) {
    int numberOfColums = 2;
    int numberOfRows = possibilities.size();
    String[][] values = new String[numberOfRows][numberOfColums];

    for(int x=0; x<possibilities.size(); x++) {
        TeamMenuSelected item = possibilities.get(x);
        values[x][0] = item.getTeamName();
        values[x][1] = item.getTeamCuisine();
    }

    return values;
}

Comments

0
 ArrayList<String> arrayList = new ArrayList<String>();
    arrayList.add("element_1");
    arrayList.add("element_2");
    arrayList.add("element_3");
    arrayList.add("element_4");
    int k=0;
    int row = 2, col = 2;
    Object[][] objArray = new Object[row][col];
     for(int i = 0 ; i < row; i++) {
         for(int j = 0; j < col; j++) {
                 objArray[i][j] = arrayList.get(k);
                 k++;
                 if(k > arrayList.size()) {
                     break;
                 }
         }
     }
     for(int i = 0 ; i < row; i++) {
         for(int j = 0; j < col; j++) {

             System.out.println("Row no "+i+" col no "+j+" "+objArray[i][j] );
         }
  }
 }

2 Comments

So Above is a way to convert your ArrayList<string> to a 2D Array. Above i have converted ArrayList<string> into 2D Object. For converting it into 2D string or 2D array just replace above code with String[][] for 2D string and Array[][] for 2D Array.
You should write the explanation in the answer itself instead of writing it in the comments.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.