Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Two dimensional array list

Tags:

java

I've heard of using a two dimensional array like this :

String[][] strArr; 

But is there any way of doing this with a list?

Maybe something like this?

ArrayList<String><String> strList; 

And using something like this to add to it?

strList.add("hey", "hey"); 

Any way of doing something like this? Any help appreciated.

It would be good if there is because i am currently putting strings into two differrent ArrayList's in pairs.

like image 319
FabianCook Avatar asked Jun 02 '12 21:06

FabianCook


People also ask

Is a 2D array a list?

The most common and the simplest form of Multidimensional Array Lists used is 2-D Array Lists.

What are two-dimensional lists?

A two-dimensional list can be considered as a “list of lists”. A two-dimensional list can be considered as a matrix where each row can have different lengths and supports different data types.

Which is an example of two-dimensional array?

Example: int A[10][20]; Here we declare a two-dimensional array in C, named A which has 10 rows and 20 columns.

Can we make 2D ArrayList?

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.


2 Answers

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();          }       });    } } 
like image 65
Hovercraft Full Of Eels Avatar answered Sep 22 '22 23:09

Hovercraft Full Of Eels


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");  } 
like image 33
user3606336 Avatar answered Sep 21 '22 23:09

user3606336