# Thread: How do I place these numbers into an array

1. Member
Join Date
Feb 2009
Posts
38
Rep Power
0

## How do I place these numbers into an array

Hello,

I am struggling with a fairly simple problem which I hope you can help with. I am working on a some code but there is a small part of it which I can't seem to get to work.

I have two classes. I want to enter details into one, which will place those details into an array which can then be accessed.

Class one:
Java Code:
```public class Triangle {

int x, y, l;
int[] cx;
int[] cy;

public Triangle(int x, int y, int l) {
this.x = x;
this.y = y;
this.l = l;
}

public void cords(){
cy[0] = x;
cx[1] = x+10;
cx[2] = x+20;

cy[0] = y;
cy[1] = y+20;
cy[2] = y;

}
}```
Then I have a class to test it:
Java Code:
```public class Test {

public static void main(String[] args) {

Triangle tos = new Triangle(100, 200, 3);

System.out.println(tos.cy[0]);

}
}```
Last edited by mainy; 07-18-2009 at 04:15 PM.

2. Before you can use an array you must instantiate it:
Java Code:
```int[] x;               // declaration
int[] y = new int[4];  // declaration and instantiation```
Java Code:
```public class TriangleRx {

int x, y, l;
int[] cx;
int[] cy;

public TriangleRx(int x, int y, int l) {
this.x = x;
this.y = y;
this.l = l;
System.out.println("cx = " + cx + "  cy = " + cy);
}

public void cords() {
cy[0] = x;
cx[1] = x+10;
cx[2] = x+20;

cy[0] = y;
cy[1] = y+20;
cy[2] = y;
}

public static void main(String[] args) {
TriangleRx test = new TriangleRx(2, 3, 5);
//        test.cords();
}
}```

3. Member
Join Date
Jul 2009
Posts
12
Rep Power
0
u r using the array without instantiating it. just add

cx=new int[size of array];
cy=new int[]size of array];

in constructor and the job is done......

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•