flexible arrays in java
Author |
Message |
Asok
|
Posted: Sun Jun 05, 2005 1:33 am Post subject: flexible arrays in java |
|
|
I was browsing though both, the tutorial and help section and I can't seem to find an example of a flexible array. (where the elements in the array are a variable and can add new ones ie. int intArray[intExpandingVar]) |
|
|
|
|
|
Sponsor Sponsor
|
|
|
Hikaru79
|
Posted: Sun Jun 05, 2005 1:38 am Post subject: (No subject) |
|
|
I wrote a Tutorial on it a few weeks back. Here it is: http://www.compsci.ca/v2/viewtopic.php?t=7070
They're called Vectors, like in C++. However, since the time I've written it, I've been introduced to the Collections framework which is a better solution for anything more serious. I would only use vectors for small things like parsing conf files where you don't know how many args there will be, and since file readers can't go back in a stream, its too much of a hassle to count. For anything else, there's Collections: http://java.sun.com/docs/books/tutorial/collections/
Enjoy, Asok |
|
|
|
|
|
rizzix
|
Posted: Sun Jun 05, 2005 1:55 am Post subject: (No subject) |
|
|
Vector is part of the collection framework.. but just a note a better alternative to Vector could be ArrayList as it does a better job following the Collection framework design.
NOTE: Vectors also confirms to the Collection framework as well. Just that if you are going to use the Vector class make sure u use add instead of addElement and get instead of elementAt, and etc..
those other methods (like elementAt etc..) are there cuz of backward compatibility. If ur curious the only diffrence between ArrayList and Vector is that the Vector is thread safe. But that does not mean you can't get a thread save version of an ArrayList (yea u can see the redundancy here?)
I suggest in most cases its best you just use ArrayList. |
|
|
|
|
|
Asok
|
Posted: Sun Jun 05, 2005 8:14 am Post subject: (No subject) |
|
|
Thanks guys
is there sample code of ArrayList anywhere? |
|
|
|
|
|
Hikaru79
|
|
|
|
|
wtd
|
Posted: Sun Jun 05, 2005 12:43 pm Post subject: (No subject) |
|
|
Perhaps this would also be a good place to demonstrate anonymous inner classes.
Java: | public class Test
{
public static void main (String[] args )
{
List list = new ArrayList();
list. add("abc");
list. add("DEF");
list. add("ghi");
// standard sort
Collections. sort(list );
Iterator iter = list. iterator();
while (iter. hasNext())
System. out. println(iter. next());
// sort, ignoring case
Collections. sort(list, new Comparator()
{
public int compare (Object o1, Object o2 )
{
return ((String)o1 ). compareToIgnoreCase((String)o2 );
}
});
iter = list. iterator();
while (iter. hasNext())
System. out. println(iter. next());
}
} |
|
|
|
|
|
|
rizzix
|
Posted: Sun Jun 05, 2005 1:28 pm Post subject: (No subject) |
|
|
Or even better if ur using 1.5:
Java: | public class Test
{
public static void main (String[] args )
{
List<String> strArray = Arrays. asList("abc", "DEF", "ghi");
Collections. sort(strArray );
for (String s : strArray )
System. out. println(s );
Comparator<String> compare_ingnore_case_String = new Comparator<String> ()
{
public int compare (String o1, String o2 ) {
return o1. compareToIgnoreCase(o2 );
}
};
Collections. sort(strArray, compare_ingnore_case_String );
for (int i = 0; i < strArray. size(); i++ )
System. out. println(strArray. get(i ));
}
} |
|
|
|
|
|
|
Asok
|
Posted: Sun Jun 05, 2005 11:19 pm Post subject: (No subject) |
|
|
thanks for the help!
I'm going to test this out tomorrow night. Can I use ArrayList for multidimensional arrays?
ie. I have a multidimensional array with fixed column elements [10] but the rows need to be flexible to allow for additional entries. |
|
|
|
|
|
Sponsor Sponsor
|
|
|
rizzix
|
Posted: Sun Jun 05, 2005 11:29 pm Post subject: (No subject) |
|
|
yea just create a List of List of whatever (in this case String): Java: | List<List<String>> strArray = Arrays. asList(
Arrays. asList("abc", "DEF", "ghi"),
Arrays. asList("hello", "world")
); |
now to get "world" we do this: Java: | strArray.get(1).get(1); |
|
|
|
|
|
|
wtd
|
Posted: Mon Jun 06, 2005 12:11 am Post subject: (No subject) |
|
|
rizzix wrote: yea just create a List of List of whatever (in this case String): Java: | List<List<String>> strArray = Arrays. asList(
Arrays. asList("abc", "DEF", "ghi"),
Arrays. asList("hello", "world")
); |
Wow. Leave it to Java to have something so fundamental not look like:
code: | [
["abc", "DEF", "ghi"],
["hello", "world"]
] |
|
|
|
|
|
|
rizzix
|
Posted: Mon Jun 06, 2005 8:50 am Post subject: (No subject) |
|
|
you could also do: Java: | List<String []> strArray = Arrays. asList(
{"abc", "DEF", "ghi"},
{"hello", "world"}
); |
|
|
|
|
|
|
|
|