2

Well I'm trying to initialize more than one array using this method and it gives me this error as mentioned: Any solutions

            int [] x,y,z=new int [10];
            System.out.println("The first element of x:"+x[0]);
            System.out.println("The second element of y:"+y[1]);
            System.out.println("The third element of z:"+z[2]);`

And the error is:

ex1.java:47: error: variable x might not have been initialized
    System.out.println("The first element of x:"+x[0]);
                                                 ^
ex1.java:48: error: variable y might not have been initialized
    System.out.println("The second element of y:"+y[1]);
                                                  ^
2 errors
2
  • I'm going to assume that you intended to include the error message, it didn't make it in for some reason, and that you're about to edit your question to include it. Commented Aug 13, 2017 at 6:51
  • Yes that's correct. I've added the error message Commented Aug 13, 2017 at 6:53

2 Answers 2

3

Well

int [] x,y,z=new int [10];

is equivalent to:

int[] x;
int[] y;
int[] z=new int [10];

x and y are not initialized.

You should initialize all 3 arrays:

int[] x = new int [10];
int[] y = new int [10];
int[] z = new int [10];
Sign up to request clarification or add additional context in comments.

Comments

1
int [] x,y,z=new int [10];

You have only initialized z in this line, not x nor y. You need to add additional initializations for the other two variables as well.

int [] x=new int [10], y=new int [10], z=new int [10];

5 Comments

This is the basic way. But the problem is, assume that I want to initialize more than 10 1-D array in one line, then what shall be the code?
Trust me, you really don't want to initialize more than 10 arrays in a single line.
But I want the code??If there is any for my problem...
Well, if you really want it, just take the code I gave you, and add to it with exactly the same pattern. It will be nasty, but it will be a single line of nasty.
Unless what you're really after is a 2D array.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.