0

Assuming whe have 8 instances of a class and 8 arrays outside the class. Something like:

float[] t1,t2,t3,t4,t5,t6,t7,t8;


public void myclass{

How can we access the array fields from inside the class? So that the first instance of the class corresponds to the first array "t1" the second to "t2" etc.

}

for (int i = 0; i < 8; i++) {
        classID[i] = new myclass(i);
    }
5
  • 4
    There is no 'outside the class' in java :-) Commented Nov 15, 2011 at 10:33
  • What do you mean by "8 arrays outside the class"? Those arrays have to be somewhere. Where they are will dictate how/if you can access them. Commented Nov 15, 2011 at 10:33
  • 1
    The class should be constructing the float[]. I would use double[] unless you have a very good reason. Commented Nov 15, 2011 at 10:34
  • in java, how can we have some array outside the class ? isnt this must be inside some class ? Commented Nov 15, 2011 at 10:34
  • It is not clear for me. How many classes do you have? If this is the only one then you cannot have an array outside of it. Commented Nov 15, 2011 at 10:36

5 Answers 5

1

in Java, if it is out side of class it should be in other class, so you need object of that class to access that array

class Foo{
  private int arr[];
  public int[] getArr(){
    return this.arr;
  }
}
//from other class now

new Foo().getArr();

or else it needs to be static

class Foo{
  public static int arr[];
}

//from other class
int arr[] = Foo.arr;
Sign up to request clarification or add additional context in comments.

1 Comment

You are welcome (also don't forget to upvote & accept, if this is actually working answer for you) :)
1

Each instance of class should contain one array. The array can be passed in the constructor, like this:

class MyClass {
    private float[] t;

    public MyClass(float[] aT) {
        t = aT;
    }
}

Then, when you create your instances, pass appropriate arrays to constructor calls:

MyClass myClass1 = new MyClass(t1);
MyClass myClass2 = new MyClass(t2);
...

Comments

0

Do you mean?

MyClass[] arrays = new MyClass[8];
for(int i=0;i<arrays.length;i++) arrays[i] = new MyClass();


class MyClass {
    final double[] values = new double[10];

}

Comments

0

There is no 'outside the class' in java So what i get u.. is u have a class in which there is 8 Float type Array u made 8 different instance of your class ..
And through each Object u want to access a different Object because "sequence of object is depend on sequence in which they are instantiated "
So for that :-
U can make a int counter as static and every time in the constructor of class u increment the counter and store that value . And access Array of respected value of counter..

Comments

0

You will need to declare an ArrayList of float arrays like

ArrayList<float[]> arrayList = new ArrayList<float[]>;

Then add each of your arrays to the arrayList. Each class will access arrayList.get(i).

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.