J
J
Just_for_students2017-10-14 14:56:36
Java
Just_for_students, 2017-10-14 14:56:36

What is the error in java array?

I declare an array, initialize it in a method, call it in the main function, and there are all zeros. Where is the mistake?

public class Not {
  int[] not = new int[10000];
  Random random = new Random();

  public int[] notArr() {
    for(int i = 0; i < not.length; i++) {
    not[i] = random.nextInt(10000) + 1;	
    }
    return not;
  }
}
public class MainTest {

  public static void main(String[] args) {
    Not testNot = new Not();
    int[] masNot = testNot.not;
                System.out.println(Arrays.toString(masNot));
}

}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Denis Zagaevsky, 2017-10-14
@Just_for_students

You didn't call the notArr method. An array is initialized to zero by default.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question