-3

Why is this array not working?

I'm taking Java as a class and this array isn't working, can you let me know why it's not working? I would have searched if this question was already asked, but I think it's just a super basic question and I don't think anyone has asked it before.

Here's the array:

class identify
{
    public static void main(String arge[]);
    {
        int a[]= new a[4];
        a[]={12,14,15,17,21};
        System.out.println(a[3]);
        System.out.println(a[4]);
    }
}

http://ideone.com/cvD1I7

  • 2
    Lose the semicolon at the end of your `main` method declaration. – rgettman Feb 04 '14 at 17:01
  • 2
    You are also allocating memory for only 4 items, and then trying to put 5 items in the array? Are you trying to redeclare `a` as an array that holds those 5 elements? – AndyG Feb 04 '14 at 17:02
  • You should also look at this stackoverflow answer for how to declare an array: http://stackoverflow.com/questions/1200621/how-to-declare-an-array-in-java – mdewitt Feb 04 '14 at 17:12

3 Answers3

2

There are a couple of errors in your code:

  • Declaration: The assignment operator = has two operands: right-side, which is the variable receiving the value, and left-side, which is the value being assigned. The left-side expression must construct an array. You will probably note that new a[4] isn't a valid construction for an array. It should be new int[4] instead.

  • Initialization: The {x,y,...} syntax can only be used when initializing an array. new int[x]; will allocate a new array with capacity/length x, and initialize every element in the array with a default value (0 in the case of ints). Once there, you can not initialize the array again with constant values with the {x,y,z} construct.

Also, the way you've declared the array (int a[]) is discouraged, use int[] a instead. It's a clearer syntax.

Try this:

int[] a = new int[]{ 12,14,15,17,21};

Note you could also have done:

// Declaring a is an array of ints
int[] a;                         
// Initialization and assignment to a. 
// Notice the array length isn't explicitly given.
a = new int[]{ 12,14,15,17,21 };

Or else:

// Declaring a is an array of ints
int[] a;                         
// Initializing a as an empty array with 5 elements (all of them will be 0)
a = new int[5];
a[0] = 12;
a[1] = 14;
a[2] = 15;
a[3] = 17;
a[4] = 21;

You might find the Arrays page of the Java Tutorials useful.

EDIT As a side note, as rgettman pointed out in his comment, there shouldn't be a semicolon at the end of the void method signature. And take into account that if you allocate a new int[4], the last valid index to access it will be 3 (a[3]). a[4] will throw an ArrayIndexOutOfBoundsException.

Xavi López
  • 26,413
  • 11
  • 94
  • 146
0

a[] is not a variable. try this:

a = new int[]{12,14,15,17,21};

evanchooly
  • 5,658
  • 1
  • 14
  • 22
0

Try this

 int a[]= new int[]{12,14,15,17,21};// initialization at the time of declaration

or you can use

    int[] a;    //declaration

  a = new int[]{12,14,15,17,21};  // then initialization
Girish
  • 1,667
  • 1
  • 16
  • 28