
-----------------------------------
Asok
Sun Jun 05, 2005 1:33 am

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])

-----------------------------------
Hikaru79
Sun Jun 05, 2005 1:38 am


-----------------------------------
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
Sun Jun 05, 2005 1:55 am


-----------------------------------
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.  :roll:

-----------------------------------
Asok
Sun Jun 05, 2005 8:14 am


-----------------------------------
Thanks guys  :D 

is there sample code of ArrayList anywhere?

-----------------------------------
Hikaru79
Sun Jun 05, 2005 11:06 am


-----------------------------------
Well, the API is here: http://java.sun.com/j2se/1.4.2/docs/api/java/util/ArrayList.html

And here's a sample little program that I got from http://java.sun.com/developer/technicalArticles/Collections/Using/ :
  import java.util.*;
  
  public class sort1 implements Comparator {
  public int compare(Object o1, Object o2)
  {
    String s1 = (String)o1;
    String s2 = (String)o2;
  
    return s1.toLowerCase(
    ).compareTo(s2.toLowerCase());
  }
  
  public static void main(String args
:)

-----------------------------------
wtd
Sun Jun 05, 2005 12:43 pm


-----------------------------------
Perhaps this would also be a good place to demonstrate anonymous inner classes.

public class Test
{
   public static void main(String

-----------------------------------
rizzix
Sun Jun 05, 2005 1:28 pm


-----------------------------------
Or even better if ur using 1.5:

public class Test
{
   public static void main(String

-----------------------------------
Asok
Sun Jun 05, 2005 11:19 pm


-----------------------------------
thanks for the help!  :D 

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.

-----------------------------------
rizzix
Sun Jun 05, 2005 11:29 pm


-----------------------------------
yea just create a List of List of whatever (in this case String):List strArray = Arrays.asList(
    Arrays.asList("abc", "DEF", "ghi"),
    Arrays.asList("hello", "world")
);

now to get "world" we do this:strArray.get(1).get(1);

-----------------------------------
wtd
Mon Jun 06, 2005 12:11 am


-----------------------------------
yea just create a List of List of whatever (in this case String):List 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:

[
   ["abc", "DEF", "ghi"],
   ["hello", "world"]
]

-----------------------------------
rizzix
Mon Jun 06, 2005 8:50 am


-----------------------------------
you could also do:List