I want to create a 2D array that each cell is an ArrayList!
If you want to create a 2D array of ArrayList.Then you can do this :
ArrayList[][] table = new ArrayList[10][10];
table[0][0] = new ArrayList(); // add another ArrayList object to [0,0]
table[0][0].add(); // add object to that ArrayList
Answer from AllTooSir on Stack OverflowI want to create a 2D array that each cell is an ArrayList!
If you want to create a 2D array of ArrayList.Then you can do this :
ArrayList[][] table = new ArrayList[10][10];
table[0][0] = new ArrayList(); // add another ArrayList object to [0,0]
table[0][0].add(); // add object to that ArrayList
The best way is to use a List within a List:
List<List<String>> listOfLists = new ArrayList<List<String>>();
How do I create a 2d ArrayList in java?
java - Two dimensional array list - Stack Overflow
How to create a 2D ArrayList - Processing Forum
Java 2D ArrayList - Free Support Forum - aspose.com
Videos
I want to implement breadth first search in java. However I am stuck at making a adjacency list of the graph. The method on GeeksforGeeks declares the list as LinkedList<Integer> adj[]; Well this is actually an array of List and not dynamic too. Like you'll have to change the array size when you want to add some extra node. So, I tried List<List<Integer>> adj = new ArrayList<List<Integer>>(); and then made the inner ArrayList in a for loop as adj.add(new ArrayList<Integer>);. I am trying from so long still getting this error 'List cannot be resolved to a type'. I searched on Stack overflow and there too they have the same declaration as mine so I don't really know what I am doing wrong. Please help me.
Edit: I am using Java 11
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();
}
});
}
}
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");
}
Hello there fine java community. So my project is to make the game Memory in java, now im not going to bore you with all the details and all my classes. But i have a problem right at the very start, i need a two dimensional array, and I will be using an ArrayList as it needs to be dynamic. The size can change based on how big the user wants it. Now i was thinking something like this:
List<List<E>> array;
To declarate a twodimensional arraylist called "array", and initialize it in the constructor.
This is my question basically, how would you do it?
Edit: Typo.
Edit: For clarification, the array will hold objects of a certain type. Meaning if it was an generic array the:
array[x][y];
Would return the object that lays on the spot x, y.