I two classes, A and B. I have a method demo() in class A, that creates an array of objects and by using a previous array1 of ints to get a value 'v3' I will create 'array2' of array1 length. And hence create a new object of array2 with parameters v1,v2,v3. I also have some getter/setters in class B for v1/2/3. I then want to access each element like
System.out.print(array2[0].getValue1());
outside the method or atleast know that it was created successfully,. But I get an error straight away by trying to print out a value, so I'm guessing not having a toString method would be the problem. I have created a variable outside the method as:
private B[] array2;
I then call in the main:
B array2 = new B[array1.length];
demo();
System.out.print(array2[0].getValue1());
This is the method
public void demo(){
int v1= 2;
int v2= 2;
int v3= 0;
B[] array2 = new B[array1.length];
for(int x = 0; x<array2.length; x++)
{
v3 = array1[x];
array1[x] = new B(v1,v2,v3);
array1[x].setValue2(v1);
array1[x].setValue2(v2);
array1[x].setValue3(v3);
v1++;
}
} It also seems I have to restate
B[] array2 = new B[array1.length];
in the method or I get an error. This seems like a similar problem