0

So i have this class and i want the coordinates of my shape to be an array but i want to be able to create a shape in my main class and set the coords there. I cant seem to get this right i dont know how to add the array coords as a variable in s1 i want to make an empty array with 2 slots called coords in class Shape. Then i want to be able to fill said array in class main with different numbers for different shapes.

  public class Shape 
  {
    private String name;
    private int coords[]={0,0};
    private double a_p;

    public Shape(String name, int[] coords, double a_p)
    {
        this.name = name;
        this.coords = coords;
        this.a_p = a_p;
    }

this is my main:

   public static void Main(String[] args) 
   {
        Shape s1=new Shape("Circle",{3, 4},5);
        
        System.out.println(s1.getCoords());
    }
 
    
Gelya
  • 11
  • 1
  • 3
    `new int[]{3, 4}` – OH GOD SPIDERS Jun 17 '22 at 16:00
  • Does this answer your question? [Array declaration and initialization in Java](https://stackoverflow.com/questions/67782405/array-declaration-and-initialization-in-java) – OH GOD SPIDERS Jun 17 '22 at 16:00
  • the thing is i want to make lots of shapes so i need to change the coords and not create a new array every time – Gelya Jun 17 '22 at 16:06
  • @Gelya well, if you want to create lots of shapes you will have to create an array for each shape. There is simply no way around it. Creating a single array that all your shapes share and that you constantly modify is not a solution that will work, and it wouldn't even make the code more compact anyway. – OH GOD SPIDERS Jun 17 '22 at 16:07
  • okay thanks for the record i am not trying to make my life hard by making coords an array instead of 2 variables like x and y. its just that my teacher gave us an exam and it says he wants the coords to be an array. I probably didnt understand correctly i guess – Gelya Jun 17 '22 at 16:11

0 Answers0