Two dimensional array list
You would use
List<List<String>> listOfLists = new ArrayList<List<String>>();
And then when you needed to add a new "row", you'd add the list:
listOfLists.add(new ArrayList<String>());
I've used this mostly when I wanted to hold references to several lists of Point in a GUI so I could draw multiple curves. It works well.
For example:
import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Point;
import java.awt.RenderingHints;
import java.awt.Stroke;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.ArrayList;
import java.util.List;
import javax.swing.*;
@SuppressWarnings("serial")
public class DrawStuff extends JPanel {
private static final int PREF_W = 400;
private static final int PREF_H = PREF_W;
private static final Color POINTS_COLOR = Color.red;
private static final Color CURRENT_POINTS_COLOR = Color.blue;
private static final Stroke STROKE = new BasicStroke(4f);
private List<List<Point>> pointsList = new ArrayList<List<Point>>();
private List<Point> currentPointList = null;
public DrawStuff() {
MyMouseAdapter myMouseAdapter = new MyMouseAdapter();
addMouseListener(myMouseAdapter);
addMouseMotionListener(myMouseAdapter);
}
@Override
public Dimension getPreferredSize() {
return new Dimension(PREF_W, PREF_H);
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2 = (Graphics2D) g;
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
g2.setStroke(STROKE);
g.setColor(POINTS_COLOR);
for (List<Point> pointList : pointsList) {
if (pointList.size() > 1) {
Point p1 = pointList.get(0);
for (int i = 1; i < pointList.size(); i++) {
Point p2 = pointList.get(i);
int x1 = p1.x;
int y1 = p1.y;
int x2 = p2.x;
int y2 = p2.y;
g.drawLine(x1, y1, x2, y2);
p1 = p2;
}
}
}
g.setColor(CURRENT_POINTS_COLOR);
if (currentPointList != null && currentPointList.size() > 1) {
Point p1 = currentPointList.get(0);
for (int i = 1; i < currentPointList.size(); i++) {
Point p2 = currentPointList.get(i);
int x1 = p1.x;
int y1 = p1.y;
int x2 = p2.x;
int y2 = p2.y;
g.drawLine(x1, y1, x2, y2);
p1 = p2;
}
}
}
private class MyMouseAdapter extends MouseAdapter {
@Override
public void mousePressed(MouseEvent mEvt) {
currentPointList = new ArrayList<Point>();
currentPointList.add(mEvt.getPoint());
repaint();
}
@Override
public void mouseDragged(MouseEvent mEvt) {
currentPointList.add(mEvt.getPoint());
repaint();
}
@Override
public void mouseReleased(MouseEvent mEvt) {
currentPointList.add(mEvt.getPoint());
pointsList.add(currentPointList);
currentPointList = null;
repaint();
}
}
private static void createAndShowGui() {
JFrame frame = new JFrame("DrawStuff");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(new DrawStuff());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGui();
}
});
}
}
Declaring a two dimensional ArrayList:
ArrayList<ArrayList<String>> rows = new ArrayList<String>();
Or
ArrayList<ArrayList<String>> rows = new ArrayList<>();
Or
ArrayList<ArrayList<String>> rows = new ArrayList<ArrayList<String>>();
All the above are valid declarations for a two dimensional ArrayList!
Now, Declaring a one dimensional ArrayList:
ArrayList<String> row = new ArrayList<>();
Inserting values in the two dimensional ArrayList:
for(int i=0; i<5; i++){
ArrayList<String> row = new ArrayList<>();
for(int j=0; j<5; j++){
row.add("Add values here");
}
rows.add(row);
}
fetching the values from the two dimensional ArrayList:
for(int i=0; i<5; i++){
for(int j=0; j<5; j++){
System.out.print(rows.get(i).get(j)+" ");
}
System.out.println("");
}
A 2d array is simply an array of arrays. The analog for lists is simply a List
of List
s.
ArrayList<ArrayList<String>> myList = new ArrayList<ArrayList<String>>();
I'll admit, it's not a pretty solution, especially if you go for a 3 or more dimensional structure.
You can create a list,
ArrayList<String[]> outerArr = new ArrayList<String[]>();
and add other lists to it like so:
String[] myString1= {"hey","hey","hey","hey"};
outerArr .add(myString1);
String[] myString2= {"you","you","you","you"};
outerArr .add(myString2);
Now you can use the double loop below to show everything inside all lists
for(int i=0;i<outerArr.size();i++){
String[] myString= new String[4];
myString=outerArr.get(i);
for(int j=0;j<myString.length;j++){
System.out.print(myString[j]);
}
System.out.print("\n");
}