Showing posts with label Array. Show all posts
Showing posts with label Array. Show all posts

Tuesday, November 26, 2013

Published 11/26/2013 by with 0 comment

java.lang.UnsupportedOperationException at java.util.AbstractList.remove

When you create a java.util.List object from an array using Arrays.asList method and after that if you try to clear or remove the elements from the list you'll get this run time error. The reason is that Arrays.asList method returns a fixed-size list backed by the specified array.

See in the example code below:

int[] myArray = {5,10,15,20};

List intList = Arrays.asList(myArray);

intList.clear();

Or

int[] myArray = {5,10,15,20};

List intList = Arrays.asList(myArray);

for(int i=0;i
{
intList.remove(i);
}

You'll get error from intList.clear() or intList.remove(i) because you cannot remove from a fixed-size array. And also you cannot add any new element in this list.

If you require to copy array elements in a list object and later need to add/delete elements from the list, you can do it in the following way.

  int[] myArray = {5,10,15,20};
List intList = new ArrayList();
System.out.println(intList); // print empty list
//copy the array elements into the list
for(int i=0;i
{
intList.add(myArray[i]);
}
System.out.println(intList); // print list after copying array elements
//You can now add
intList.add(25);
System.out.println(intList); // print list after adding new element
//You can remove
intList.remove(0);
System.out.println(intList); // print list after removing an element
//You can remove all the elements
intList.clear();
System.out.println(intList); // print list after removing all the elements

Below is the output of this sample code:

[]
[5, 10, 15, 20]
[5, 10, 15, 20, 25]
[10, 15, 20, 25]
[]

Read More
    email this       edit
Published 11/26/2013 by with 0 comment

Dimensions expected after this token / '[' expected

Java error "Dimensions expected after this token" or " '[' expected " can occur while creating array elements or allocating memory using the keyword new. If the square brackets [] are not used properly, you can get this error message.

See the code below:

int myArray[]=new int{10,20,30,40};

Here this is a syntax error. You have to put square brackets [] after new int. To initialize an array the correct code would be:

int myArray[]=new int[]{10,20,30,40};

or just

int myArray[]={10,20,30,40};
Read More
    email this       edit
Published 11/26/2013 by with 0 comment

Cannot invoke length() on the array type

The error message "Cannot invoke length() on the array type" or "cannot find symbol ... method length()" is related with the use of array.

Java array has a built in property length which is a variable, not method. If you put parenthesis () after the array property length, you'll get this error.

See the example below:

int[] number = {5,10,15};

for(int i=0; i<number.length(); i++)
{
    System.out.println(number[i]);
}

Here number.length() is wrong. To resolve the error, remove the parenthesis ().

The correct code:

for(int i=0; i<number.length; i++)
{
    System.out.println(number[i]);
}

Read More
    email this       edit

Friday, October 30, 2009

Published 10/30/2009 by with 0 comment

java.lang.ArrayIndexOutOfBoundsException


It's a very common runtime error message or exception we face when we use array wrongly. Very specifically this exception is thrown to indicate that an array has been accessed with an illegal index (location / position / subscript). The index is either negative or greater than or equal to the size of the array.


See the following code:

int data[]={5,7,18,21,27,15,3};

//show the array elements
for(int i=0;i<=7;i++)
{
System.out.println(data[i]);
}

Here the array size is 7 as the array is initialized by assigning 7 elements {5,7,18,21,27,15,3}. So the valid array index is 0 to 6. In the loop condition, we have given i&lt=7 and i is used as the array index inside the loop - so when i is 7 it tries to access the array element 7 but 7 is not a valid index. This causes the exception. Correct loop condition is i<7 or i<=6. The recommended approach is using the array built in variable length instead of the static size. Correct the code as below:


int data[]={5,7,18,21,27,15,3};

//show the array elements
for(int i=0;i<data.length;i++)//data is the array name
{
System.out.println(data[i]);
}

Read More
    email this       edit
Published 10/30/2009 by with 0 comment

array dimension missing

'array dimension missing' is shown when the array size is not specified while allocating memory for the array or creating an array.

See below:

int data[ ]=new int[ ];

See that, blank [ ] is given in new int[ ]. We must give an int as the size of the array. Memory will be allocated for the array according to the given int within the [ ] as below:

int data[]=new int[5];
Read More
    email this       edit
Published 10/30/2009 by with 7 comments

illegal initializer

You may see this error for writing wrong expression to initialize an array. See the code below:

int data={42,15,27,20,19};

If you compile this, you will see the following error message:

illegal initializer for int
int data={42,15,27,20,19};
^

Here the array is not declared properly as we have not put [ ] for declaring the array. The correct code is:

int data[ ]={42,15,27,20,19};

or

int[ ] data={42,15,27,20,19};
Read More
    email this       edit