I know that's an old question with good answers, but I believe I can add my 2 cents.
The simplest and most flexible way which works for me is just using an almost "Plain and Old Java Object" class2D to create each "row" of your array.
The below example has some explanations and is executable (you can copy and paste it, but remember to check the package name):
package my2darraylist;
import java.util.ArrayList;
import java.util.List;
import javax.swing.JPanel;
public class My2DArrayList
{
public static void main(String[] args)
{
// This is your "2D" ArrayList
//
List<Box> boxes = new ArrayList<>();
// Add your stuff
//
Box stuff = new Box();
stuff.setAString( "This is my stuff");
stuff.addString("My Stuff 01");
stuff.addInteger( 1 );
boxes.add( stuff );
// Add other stuff
//
Box otherStuff = new Box();
otherStuff.setAString( "This is my other stuff");
otherStuff.addString("My Other Stuff 01");
otherStuff.addInteger( 1 );
otherStuff.addString("My Other Stuff 02");
otherStuff.addInteger( 2 );
boxes.add( otherStuff );
// List the whole thing
for ( Box box : boxes)
{
System.out.println( box.getAString() );
System.out.println( box.getMyStrings().size() );
System.out.println( box.getMyIntegers().size() );
}
}
}
class Box
{
// Each attribute is a "Column" in you array
//
private String aString;
private List<String> myStrings = new ArrayList<>() ;
private List<Integer> myIntegers = new ArrayList<>();
// Use your imagination...
//
private JPanel jpanel;
public void addString( String s )
{
myStrings.add( s );
}
public void addInteger( int i )
{
myIntegers.add( i );
}
// Getters & Setters
public String getAString()
{
return aString;
}
public void setAString(String aString)
{
this.aString = aString;
}
public List<String> getMyStrings()
{
return myStrings;
}
public void setMyStrings(List<String> myStrings)
{
this.myStrings = myStrings;
}
public List<Integer> getMyIntegers()
{
return myIntegers;
}
public void setMyIntegers(List<Integer> myIntegers)
{
this.myIntegers = myIntegers;
}
public JPanel getJpanel()
{
return jpanel;
}
public void setJpanel(JPanel jpanel)
{
this.jpanel = jpanel;
}
}
UPDATE - To answer the question from @Mohammed Akhtar Zuberi, I've created the simplified version of the program, to make it easier to show the results.
import java.util.ArrayList;
public class My2DArrayListSimplified
{
public static void main(String[] args)
{
ArrayList<Row> rows = new ArrayList<>();
Row row;
// Insert the columns for each row
// First Name, Last Name, Age
row = new Row("John", "Doe", 30);
rows.add(row);
row = new Row("Jane", "Doe", 29);
rows.add(row);
row = new Row("Mary", "Doe", 1);
rows.add(row);
// Show the Array
//
System.out.println("First\t Last\tAge");
System.out.println("----------------------");
for (Row printRow : rows)
{
System.out.println(
printRow.getFirstName() + "\t " +
printRow.getLastName() + "\t" +
printRow.getAge());
}
}
}
class Row
{
// REMEMBER: each attribute is a column
//
private final String firstName;
private final String lastName;
private final int age;
public Row(String firstName, String lastName, int age)
{
this.firstName = firstName;
this.lastName = lastName;
this.age = age;
}
public String getFirstName()
{
return firstName;
}
public String getLastName()
{
return lastName;
}
public int getAge()
{
return age;
}
}
The code above produces the following result (I ran it on NetBeans):
run:
First Last Age
----------------------
John Doe 30
Jane Doe 29
Mary Doe 1
BUILD SUCCESSFUL (total time: 0 seconds)