-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.

9
  • 1
    The question is: what do you want to do? Commented Apr 11, 2016 at 17:12
  • If you want to know if you can do it, try compiling your code and see if you get an error. Commented Apr 11, 2016 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 Commented Apr 11, 2016 at 17:17
  • Possible dup of creating arraylist of arrays Commented Apr 11, 2016 at 17:18
  • @AJNeufeld That question is about re-adding the same array reference multiple times. The OP is doing nothing like that, here. Commented Apr 11, 2016 at 17:19

3 Answers 3

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.

Sign up to request clarification or add additional context in comments.

Comments

0

This is the simplest way i guess :

new ArrayList<Double>(Arrays.asList(Test))

1 Comment

This doesn't really answer OP's question.
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;
    }
}

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.