Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 flexible arrays in java
Index -> Programming, Java -> Java Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
Asok




PostPosted: 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
Sponsor
sponsor
Hikaru79




PostPosted: 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 Smile
rizzix




PostPosted: 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. Rolling Eyes
Asok




PostPosted: Sun Jun 05, 2005 8:14 am   Post subject: (No subject)

Thanks guys Very Happy

is there sample code of ArrayList anywhere?
Hikaru79




PostPosted: Sun Jun 05, 2005 11:06 am   Post subject: (No subject)

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/ :
Java:
  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[])
  {
      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 sort1());
      iter = list.iterator();
      while (iter.hasNext())
        System.out.println(iter.next());
    }
  }

Smile
wtd




PostPosted: 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




PostPosted: 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




PostPosted: Sun Jun 05, 2005 11:19 pm   Post subject: (No subject)

thanks for the help! Very Happy

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
Sponsor
sponsor
rizzix




PostPosted: 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




PostPosted: 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




PostPosted: 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"}
);
Display posts from previous:   
   Index -> Programming, Java -> Java Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 11 Posts ]
Jump to:   


Style:  
Search: