-7

I've been trying to store an array in an array list by using a method similar to this:

static ArrayList<double []> Shapes;
public static void Test(){
    double [] Test = new double [2];
    Test[0] = 1;
    Test[1] = 2;
    Shapes.add(Test);
}

I'm not really sure if this is something you can do but if anybody knows how to work this out, it would be much appreciated.

azurefrog
  • 10,785
  • 7
  • 42
  • 56
Rm45
  • 7
  • 1
  • 1
    The question is: what do you want to do? – Tunaki Apr 11 '16 at 17:12
  • If you want to know if you *can* do it, try compiling your code and see if you get an error. – azurefrog Apr 11 '16 at 17:14
  • i want to have a list of arrays that contains info about different shapes. for example a square with a side length of 10cm, its perimeter is 40cm and area is 100cm – Rm45 Apr 11 '16 at 17:17
  • Possible dup of [creating arraylist of arrays](http://stackoverflow.com/questions/18277657/creating-arraylist-of-arrays) – AJNeufeld Apr 11 '16 at 17:18
  • @AJNeufeld That question is about re-adding the same array reference multiple times. The OP is doing nothing like that, here. – azurefrog Apr 11 '16 at 17:19
  • Storing arrays of associated data like that is the wrong way to go. Consider creating a `Shape` class that holds the required data, and then use a `List`. – azurefrog Apr 11 '16 at 17:21
  • IDE output can be very helpful. Try to understand what it's telling you. At this point you just need to initialize `Shapes` with something like `Shapes = new ArrayList<>();` – robotlos Apr 11 '16 at 17:24
  • @azurefrog - What the OP **wanted** in that question was an arraylist of arrays (but yes, their problem was they ended up adding reference multiple times). Perhaps this is a closer dup: [Java ArrayList of Arrays?](http://stackoverflow.com/questions/3642205/java-arraylist-of-arrays). – AJNeufeld Apr 11 '16 at 17:24
  • @AJNeufeld, I think OP is just having issues because OP has not initialized `Shapes` leading OP to believe that they can't do what they're trying to do. Those possible dupes don't really cover that. – robotlos Apr 11 '16 at 17:27

3 Answers3

2

Basically, this won't work because you need to call = new ArrayList<double[]>(); at some point (or without the double[] in some java versions).

If you want to store info about different shapes, you should really think about just making a class for them and then storing instances of that class though.

Mark
  • 1,498
  • 1
  • 9
  • 13
0

This is the simplest way i guess :

new ArrayList<Double>(Arrays.asList(Test))
MackTank
  • 137
  • 9
0

Your code looks nearly correct in Java syntax, although needs style changes as Java convention is to use lowercase first letters for variables and methods. And other posters have pointed out the missing 'new ArrayList' to create the array.

The problem is that from an object-oriented design point of view, using double[] to hold info about different shapes is not good design. I think what you want is and ArrayList of ShapeInfo rather than an ArrayList of double[]. ShapeInfo can be an interface which can be implemented by various classes. Something like this will allow you to have different info parameters for each shape as well as having a common method (getSummary) for all shapes that can be used in loops.

import java.util.ArrayList;
import java.util.List;

public class Shapes {
    private static List<ShapeInfo> shapes;

    public static void main(String[] args) {
        shapes = new ArrayList<>();
        ShapeInfo info = new CircleInfo(1, 2, 3);
        shapes.add(info);
        // Check what's there
        for (ShapeInfo shape : shapes) {
            System.out.println(shape.getSummary());
        }
    }
}

interface ShapeInfo {
    String getSummary();
}

class CircleInfo implements ShapeInfo {
    private double centreX, centreY, radius;

    public CircleInfo(double centreX, double centreY, double radius) {
        this.centreX = centreX;
        this.centreY = centreY;
        this.radius = radius;
    }

    @Override
    public String getSummary() {
        return "Centre is at (" + centreX + "," + centreY + ") and radius is " + radius;
    }
}
johnsgp
  • 103
  • 1
  • 8