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 5 comments

Resource leak: 'scanner' is never closed

If you do not close the java.util.Scanner object after reading data, you can get an warning saying "Resource leak: 'scanner' is never closed".

In the code below see that the Scanner object is closed in a finally block after reading all inputs using the Scanner object.

int number1, number2;

Scanner scanner = new Scanner(System.in);

try
{
System.out.println("Ener the first number");
number1 = scanner.nextInt();
System.out.println("Ener the second number");
number2 = scanner.nextInt();
}
finally
{
scanner.close();
}

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