This week's book giveaway is in the Jobs Discussion forum.
We're giving away four copies of Soft Skills: The software developer's life manual and have John Sonmez on-line!
See this thread for details.
Win a copy of Soft Skills: The software developer's life manual this week in the Jobs Discussion forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Arrays.asList(Object[] o)

 
michael huang
Ranch Hand
Posts: 63
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi, guys
when i read TIJ2 about unsupported operation, it has code like this:
String[] s={...};
List a= Arrays.asList(s);
and i look the source of jdk,
public static List asList(Object[] a) {
return new ArrayList(a);
}
so i look at the ArrayList's constructor, there is no any constructor accept array as prarameter, neither it's ancestor.
so how does List produced? can anyone explain for me?
thanks
michael
 
Jane Griscti
Ranch Hand
Posts: 3141
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Michael,
I can't answer your question but I'm really curious How did you look at the source for the JDK class??
Thanks
------------------
Jane
 
ali farid
Ranch Hand
Posts: 60
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi michael
I think you have been looking for the constructor in the java.util.ArrayList's java file.
if you look into java.util.Arrays java file then you can find an inner class "ArrayList" at the end just below the asList() method, which contains the constructor which takes the array as parameter.
the inner class which i copied from Arrays is as:
private static class ArrayList extends AbstractList
implements java.io.Serializable
{
private Object[] a;
ArrayList(Object[] array) {
a = array;
}
public int size() {
return a.length;
}
public Object[] toArray() {
return (Object[]) a.clone();
}
public Object get(int index) {
return a[index];
}
public Object set(int index, Object element) {
Object oldValue = a[index];
a[index] = element;
return oldValue;
}
}
 
michael huang
Ranch Hand
Posts: 63
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi, ali
oh, yes, thank you very much, i just look at the file java.util.ArrayList, and not find the inner class arraylist at Arrays.
and Just because of this class, so there are many "unsupported operation".
BTW Jane, you can extract java source at jkd directory using: jar -xvf src.jar.
thank you
michael
[This message has been edited by michael huang (edited October 20, 2000).]
 
Jane Griscti
Ranch Hand
Posts: 3141
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Michael.
------------------
Jane
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic