Sé que es una vieja pregunta con buenas respuestas, pero creo que puedo agregar 2 centavos.
La manera más simple y más flexible que funciona para mí es simplemente usar un class2D casi "Plain and Old Java Object" para crear cada "fila" de su matriz.
El siguiente ejemplo tiene algunas explicaciones y es ejecutable (se puede copiar y pegar, pero recuerde que debe comprobar el nombre del paquete):
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;
}
}
ACTUALIZACIÓN - Para responder a la pregunta de @Mohammed Akhtar Zuberi , He creado la versión simplificada del programa, para que sea más fácil mostrar los resultados.
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;
}
}
El código anterior produce el resultado siguiente (me encontré en NetBeans):
run:
First Last Age
----------------------
John Doe 30
Jane Doe 29
Mary Doe 1
BUILD SUCCESSFUL (total time: 0 seconds)
Así que cuando se quiere obtener la cadena desde ella se acaba de utilizar este algo así como lista de listOfLists.getIndex (0) .getIndex (0) ;? – FabianCook
@tskuzzy: gracias! –
@Smart: usaría 'listOfLists.get (index1) .get (index2)' –