Home > Archive > Java Help > December 2006 > [array / List]Unknown number of objects
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
[array / List]Unknown number of objects
|
|
| Daniel Moyne 2006-12-14, 7:10 pm |
| I want to get an array of strings from a method ; as this array has an
unknown number of elements can I use instaed a List to dynamically build
the List and then when finished transform the List into an array in
return ?
| |
|
|
Daniel Moyne wrote:
> I want to get an array of strings from a method ; as this array has an
> unknown number of elements can I use instaed a List to dynamically build
> the List and then when finished transform the List into an array in
> return ?
Sure, see:
http://java.sun.com/j2se/1.5.0/docs...st.html#toArray(T[])
Carl.
| |
| Daniel Moyne 2006-12-14, 7:10 pm |
| Carl wrote:
>
> Daniel Moyne wrote:
>
> Sure, see:
>
> http://java.sun.com/j2se/1.5.0/docs...st.html#toArray(T[])
Carl,
on my way to get a "return array" I tried to compile this found on the net :
import java.util.ArrayList;
import java.util.List;
/** List to array */
public class ToArray {
public static void main(String[] args) {
List list = new ArrayList();
list.add("Blobbo");
list.add("Cracked");
list.add("Dumbo");
// list.add(new Date()); // Don't mix and match!
// Convert a collection to Object[], which can store objects
// of any type.
Object[] ol = list.toArray();
System.out.println("Array of Object has length " + ol.length);
// This would throw an ArrayStoreException if the line
// "list.add(new Date())" above were uncommented.
String[] sl = (String[]) list.toArray(new String[0]);
System.out.println("Array of String has length " + sl.length);
}
}
but I have these error messages that do not make sense to me :
root@azun:/usr/local/Java/genj/report#
$JAVA_HOME/bin/javac -classpath /usr/local/Java/genj/report ToArray.java
Note: ToArray.java uses unchecked or unsafe operations.
Note: Recompile with -Xlint:unchecked for details.
root@azun:/usr/local/Java/genj/report#
$JAVA_HOME/bin/javac -Xlint:unchecked -classpath /usr/local/Java/genj/report
ToArray.java
ToArray.java:8: warning: [unchecked] unchecked call to add(E) as a member of
the raw type java.util.List
list.add("Blobbo");
^
ToArray.java:9: warning: [unchecked] unchecked call to add(E) as a member of
the raw type java.util.List
list.add("Cracked");
^
ToArray.java:10: warning: [unchecked] unchecked call to add(E) as a member
of the raw type java.util.List
list.add("Dumbo");
^
ToArray.java:20: warning: [unchecked] unchecked call to <T>toArray(T[]) as a
member of the raw type java.util.List
String[] sl = (String[]) list.toArray(new String[0]);
^
4 warnings
And it is supposed to work according to his author.
If you have explanations help would be appreciated.
Reagrds.
| |
| Knute Johnson 2006-12-14, 7:10 pm |
| Daniel Moyne wrote:
> Carl wrote:
>
> Carl,
> on my way to get a "return array" I tried to compile this found on the net :
>
> import java.util.ArrayList;
> import java.util.List;
>
> /** List to array */
> public class ToArray {
> public static void main(String[] args) {
> List list = new ArrayList();
> list.add("Blobbo");
> list.add("Cracked");
> list.add("Dumbo");
> // list.add(new Date()); // Don't mix and match!
>
> // Convert a collection to Object[], which can store objects
> // of any type.
> Object[] ol = list.toArray();
> System.out.println("Array of Object has length " + ol.length);
>
> // This would throw an ArrayStoreException if the line
> // "list.add(new Date())" above were uncommented.
> String[] sl = (String[]) list.toArray(new String[0]);
> System.out.println("Array of String has length " + sl.length);
> }
> }
>
> but I have these error messages that do not make sense to me :
> root@azun:/usr/local/Java/genj/report#
> $JAVA_HOME/bin/javac -classpath /usr/local/Java/genj/report ToArray.java
> Note: ToArray.java uses unchecked or unsafe operations.
> Note: Recompile with -Xlint:unchecked for details.
> root@azun:/usr/local/Java/genj/report#
> $JAVA_HOME/bin/javac -Xlint:unchecked -classpath /usr/local/Java/genj/report
> ToArray.java
> ToArray.java:8: warning: [unchecked] unchecked call to add(E) as a member of
> the raw type java.util.List
> list.add("Blobbo");
> ^
> ToArray.java:9: warning: [unchecked] unchecked call to add(E) as a member of
> the raw type java.util.List
> list.add("Cracked");
> ^
> ToArray.java:10: warning: [unchecked] unchecked call to add(E) as a member
> of the raw type java.util.List
> list.add("Dumbo");
> ^
> ToArray.java:20: warning: [unchecked] unchecked call to <T>toArray(T[]) as a
> member of the raw type java.util.List
> String[] sl = (String[]) list.toArray(new String[0]);
> ^
> 4 warnings
>
> And it is supposed to work according to his author.
>
> If you have explanations help would be appreciated.
> Reagrds.
>
Daniel:
You are having problems because you appear to be using a compiler newer
than when the sample program was written. Take a look at mine below and
I think it will solve all of your questions.
import java.util.*;
public class test {
public static void main(String[] args) {
ArrayList<String> list = new ArrayList<String>();
list.add("Blobbo");
list.add("Cracked");
list.add("Dumbo");
Object[] obj = list.toArray();
for (int i=0; i<obj.length; i++)
System.out.println((String)obj[i]);
String[] array = list.toArray(new String[list.size()]);
System.out.println("array has length " + array.length);
}
}
--
Knute Johnson
email s/nospam/knute/
| |
| Daniel Moyne 2006-12-15, 8:11 am |
| Knute Johnson wrote:
> Daniel Moyne wrote:
>
> Daniel:
>
> You are having problems because you appear to be using a compiler newer
> than when the sample program was written. Take a look at mine below and
> I think it will solve all of your questions.
>
> import java.util.*;
>
> public class test {
> public static void main(String[] args) {
> ArrayList<String> list = new ArrayList<String>();
> list.add("Blobbo");
> list.add("Cracked");
> list.add("Dumbo");
>
> Object[] obj = list.toArray();
> for (int i=0; i<obj.length; i++)
> System.out.println((String)obj[i]);
>
> String[] array = list.toArray(new String[list.size()]);
> System.out.println("array has length " + array.length);
> }
> }
Knute,
thanks for your help so basically it was a proper list of import ; error
messages provided by javac should be clearer !.
As the main objective of my question was to get a way to feed an array of
unknown number of elements and therefore use an ArrayList, here is my piece
of work where I handle data though an ArrayList and return an array ; it
works neatly.
public String[] getAllClassName(Indi indi) {
/* check for existing classes */
/* not very efficient as we do not have a class names list */
Locale frLocale = new Locale("fr","FR","fr_FR.UTF-8");
Collator myCollator = Collator.getInstance(frLocale);
ArrayList<String> classnamelist = new ArrayList<String>();
String classname;
Boolean errorflag=false;
Property classnameproperties[];
Indi indi_;
Gedcom gedcom=indi.getGedcom();
Collection indis=gedcom.getEntities(Gedcom.INDI);
for (Iterator it=indis.iterator(); it.hasNext();) {
/* we get all _CLAS properties from all INDI's */
indi_=(Indi)it.next();
/* we get all _CLAS property values from all INDI's */
classnameproperties=indi_.getProperties(ClassNameTag);
/* if no _CLAS tags we do nothing */
if (classnameproperties.length != 0) {
/* there are _CLAS tags */
/* we want to collect the _CLAS tag value */
for (int i=0; i<classnameproperties.length; i++) {
classname=classnameproperties[i].getValue();
if (!classnamelist.contains(classname)) {
/* classname not included yet in classnamelist */
/* we add it */
classnamelist.add(classname);
}
}
}
}
/* we sort the arraylist alphabetically */
Collections.sort(classnamelist,myCollator);
/* we return an array */
return classnamelist.toArray(new String[classnamelist.size()]);
}
Thanks again !
Daniel.
| |
|
|
Daniel Moyne wrote:
> Knute Johnson wrote:
> Knute,
> thanks for your help so basically it was a proper list of import ; error
> messages provided by javac should be clearer !.
** Message cut **
Not exactly Daniel,
The error message was due to the fact you were not specifying the type
of the collection List to the compiler in your original example.
You had:
List list = new ArrayList();
in your original example, when you should have written:
List<String> list = new ArrayList<String>();
See this page which gives a brief description of this:
http://java.sun.com/j2se/1.5.0/docs...e/generics.html
Hope that helps,
Carl.
| |
| Oliver Wong 2006-12-15, 7:10 pm |
| "Carl" <c.groner@gmail.com> wrote in message
news:1166212639.593019.198020@16g2000cwy.googlegroups.com...
>
> Daniel Moyne wrote:
>
> ** Message cut **
>
> Not exactly Daniel,
>
> The error message was due to the fact you were not specifying the type
> of the collection List to the compiler in your original example.
>
> You had:
> List list = new ArrayList();
> in your original example, when you should have written:
> List<String> list = new ArrayList<String>();
Another point of pedantry:
It wasn't an error message at all, but a warning. You could have also
just ignored those warnings and ran the program, and it would have behaved
correctly. The warnings, I guess, are mainly to encourage you to start using
generics, a feature newly introduced between 1.4 and 1.5.
- Oliver
| |
| Daniel Moyne 2006-12-15, 7:10 pm |
| Oliver Wong wrote:
> "Carl" <c.groner@gmail.com> wrote in message
> news:1166212639.593019.198020@16g2000cwy.googlegroups.com...
>
> Another point of pedantry:
>
> It wasn't an error message at all, but a warning. You could have also
> just ignored those warnings and ran the program, and it would have behaved
> correctly. The warnings, I guess, are mainly to encourage you to start
> using generics, a feature newly introduced between 1.4 and 1.5.
>
> - Oliver
As a matter of fact I am using this :
ArrayList<String> classnamelist = new ArrayList<String>();
so 2 questions :
(1) what is the difference with List<String>list=newArrayList<String>();
very strange as the name of the object you want to instanciate has a
different name from the name upfront (very uncommon in declaration in
Java).
(2) adding <String> declares I would assume a string content and this what I
want but can a list contains any type of objects ?
Regards.
Daniel.
| |
| Oliver Wong 2006-12-15, 7:10 pm |
| "Daniel Moyne" <dmoyne@tiscali.fr> wrote in message
news:elv50j$rqs$1@news.tiscali.fr...
>
> As a matter of fact I am using this :
> ArrayList<String> classnamelist = new ArrayList<String>();
> so 2 questions :
> (1) what is the difference with List<String>list=newArrayList<String>();
> very strange as the name of the object you want to instanciate has a
> different name from the name upfront (very uncommon in declaration in
> Java).
Inheritance typically (there are exceptions) represents an IS-A
relationship. ArrayList implements List, so you can say that an ArrayList
IS-A List, and a List IS-A Collection, and a Collection IS-A Object, etc.
So when you write (dropping the generics for a moment):
List foo = new ArrayList()
You're basically saying "I wish to declare a reference called 'foo'
whose type is List. I wish to create a new instance of ArrayList, and store
a reference to this newly created ArrayList in 'foo'."
This isn't as uncommon as you think, and I see it fairly often in Java
code. I'm a bit too tired right now to get into the reasons for using this,
but I'll just provide an example that might get you thinking about the
possiblities:
public void bar(boolean doINeedQuickRandomAccess) {
List foo = doINeedQuickRandomAccess ? new ArrayList() : new LinkedList();
/*more code*/
}
> (2) adding <String> declares I would assume a string content and this what
> I
> want but can a list contains any type of objects ?
Right. If you look at the source code for List, it looks something like:
public interface List<E extends Object> extends Collection<E>
E is a "type variable", and there's no real reason they chose "E". They
could have also chosen "J", or "Bob", or any other name. But what the
declaration is saying that that the "type parameter" (String in your case)
has to extend Object, and that a List<E> (where, again, E can be anything
that extends Object) IS-A Collection<E>. So putting in actual values into
the type variable, we have a List<String> IS-A Collection<String>. You might
read this as "A list of strings is a collection of strings".
Type variables don't necessary specify the concept of containment. The
Comparator interface also accepts a generic type argument, which specifies
what kind of objects it will compare. So a comparator which compares two
strings (perhaps in alphabetic order) would have a declaration like:
Comparator<String> myAlphabeticalComparator = new SomeClassIWrote();
- Oliver
| |
| Daniel Moyne 2006-12-16, 8:09 am |
| Oliver Wong wrote:
> "Daniel Moyne" <dmoyne@tiscali.fr> wrote in message
> news:elv50j$rqs$1@news.tiscali.fr...
>
> Inheritance typically (there are exceptions) represents an IS-A
> relationship. ArrayList implements List, so you can say that an ArrayList
> IS-A List, and a List IS-A Collection, and a Collection IS-A Object, etc.
>
> So when you write (dropping the generics for a moment):
>
> List foo = new ArrayList()
>
> You're basically saying "I wish to declare a reference called 'foo'
> whose type is List. I wish to create a new instance of ArrayList, and
> store a reference to this newly created ArrayList in 'foo'."
>
> This isn't as uncommon as you think, and I see it fairly often in Java
> code. I'm a bit too tired right now to get into the reasons for using
> this, but I'll just provide an example that might get you thinking about
> the possiblities:
>
> public void bar(boolean doINeedQuickRandomAccess) {
> List foo = doINeedQuickRandomAccess ? new ArrayList() : new
> LinkedList(); /*more code*/
> }
>
>
>
> Right. If you look at the source code for List, it looks something
> like:
>
> public interface List<E extends Object> extends Collection<E>
>
> E is a "type variable", and there's no real reason they chose "E".
> They
> could have also chosen "J", or "Bob", or any other name. But what the
> declaration is saying that that the "type parameter" (String in your case)
> has to extend Object, and that a List<E> (where, again, E can be anything
> that extends Object) IS-A Collection<E>. So putting in actual values into
> the type variable, we have a List<String> IS-A Collection<String>. You
> might read this as "A list of strings is a collection of strings".
>
> Type variables don't necessary specify the concept of containment. The
> Comparator interface also accepts a generic type argument, which specifies
> what kind of objects it will compare. So a comparator which compares two
> strings (perhaps in alphabetic order) would have a declaration like:
>
> Comparator<String> myAlphabeticalComparator = new SomeClassIWrote();
>
> - Oliver
Thanks Oliver for all this.
As I am progressing writing a pluggin I will open a new topics.
Regards.
|
|
|
|
|