Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Converting an ArrayList into a 2D Array

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

like image 947
Anto Avatar asked Jan 27 '10 11:01

Anto


People also ask

How do I turn an array into a 2D array?

Use reshape() Function to Transform 1d Array to 2d Array The number of components within every dimension defines the form of the array. We may add or delete parameters or adjust the number of items within every dimension by using reshaping.

Can we create 2D array using ArrayList in Java?

Two-Dimensional ArrayList In addition, let's assume there are 3 edges in the graph (0, 1), (1, 2), and (2, 0), where a pair of vertices represents an edge. We can represent the edges in a 2-D ArrayList by creating and populating an ArrayList of ArrayLists.

Can an ArrayList be 2D?

Arrays can be created in 1D or 2D. 1D arrays are just one row of values, while 2D arrays contain a grid of values that has several rows/columns. 1D: 2D: An ArrayList is just like a 1D Array except it's length is unbounded and you can add as many elements as you need.

Can ArrayList convert into array?

ArrayLists are resizable arrays and can store elements of type wrapper class objects. Java provides the flexibility of converting ArrayLists to Array and vice versa. 3 ways of conversion - manual conversion using get() method, using Object[] toArray() method, using T[] toArray(T[] arr) method.


2 Answers

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);
like image 115
Adam Paynter Avatar answered Nov 03 '22 22:11

Adam Paynter


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();
}
like image 39
Stephen C Avatar answered Nov 03 '22 23:11

Stephen C