Bug 201985 - Arrays.asList()
Summary: Arrays.asList()
Status: VERIFIED INVALID
Alias: None
Product: JDT
Classification: Eclipse Project
Component: Core (show other bugs)
Version: 3.3   Edit
Hardware: PC Windows XP
: P3 normal (vote)
Target Milestone: 3.4 M2   Edit
Assignee: JDT-Core-Inbox CLA
QA Contact:
URL:
Whiteboard:
Keywords:
Depends on:
Blocks:
 
Reported: 2007-09-01 16:17 EDT by D.W. CLA
Modified: 2007-09-17 08:38 EDT (History)
3 users (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description D.W. CLA 2007-09-01 16:17:14 EDT
package array;

import java.util.*;

public class ArrayListTest {
	public static void main(String[] args) {
		ArrayList al = new ArrayList();
		al.add("Mike");
		al.add("John");
		al.add("David");

		// ArrayList toArray() returns an Object[] array.
		Object[] objs = al.toArray();

		// fixed List!!! be careful.
		List l = Arrays.asList(objs);
		l.add("Eric");
		/*
		 * Arrays asList() returns a fixed-size list 
		 * Eclipse 3.3 doesn't flag a error for the above l.add("Eric"). 
		 * so I got an exception during the execution
		 * Exception in thread "main"
		 * java.lang.UnsupportedOperationException at
		 * java.util.AbstractList.add(Unknown Source) at
		 * java.util.AbstractList.add(Unknown Source) at
		 * array.ArrayListTest.main(ArrayListTest.java:17)
		 * 
		 */
		System.out.println(l);

	}
}
Comment 1 Alex Blewitt CLA 2007-09-02 04:28:55 EDT
You don't know from the type (List) that it is fixed size. It is just as likely to be a variable-sized one. So unless you can track where the collection is instantiated, you won't find it in the general case.

It doesn't sound sensible to me to have a bunch of functionality that's associated with a single call to an API, but that's just my personal opinion.
Comment 2 Olivier Thomann CLA 2007-09-02 15:35:19 EDT
This is not up to the compiler to know the semantic of the method Arrays.asList(...). It is up to the developer to know that the returned list is a fixed-size list.
Closing as INVALID.
Comment 3 Maxime Daniel CLA 2007-09-17 08:38:14 EDT
Verified for 3.4 M2 using build I20070917-0010.