Using ArrayList (Question)

Discussion in 'Java' started by Paul O'Grady, Feb 15, 2005.

  1. Paul O'Grady

    Paul O'Grady Guest

    Hi all,

    I have some code that I am converting from C to Java. I have two very
    simple structs as ff:

    struct a {
    float *data ;
    int num_elements ;
    }

    struct b {
    int *data ;
    int num_elements ;
    }

    As you can see, the only difference is that one stores an array of
    integers and the other stores an array of floats. This leads to some
    unnecessarily difficult to read code. I want to simply this in my Java
    code by storing the data in an ArrayList. I am thinking of saving the
    data as objects so that I can use the same class to store *either*
    integers or floats (or any other data tpe later on - ala C++ template
    classes).

    My proposed new object is as ff:

    public class ArrayClass {
    private ArrayList data = null ;
    .....

    ArrayClass( int NumElements)
    {
    data = new ArrayList(NumElements) ;
    ......
    }

    /* Problem is with the methods */
    int getItem(int el_num) {} ;
    float getItem(int el_num) {} ;
    }


    My two questions then are:

    1). Will I suffer a huge performance penalty because I am storing
    objects instead of native types in an array (I chose a ArayList because
    I will need to expand the array from time to time)

    2). Can I use the overloading of methods to get around the different
    datatypes (the Class will only store one data type, and needs to throw
    exceptions when an object of a different type is being added to the
    ArrayList

    3). lastly, is this the best way to implement the required functionality
    (I am a Java Newbie)


    Look forward to your answer


    Thanks
     
    Paul O'Grady, Feb 15, 2005
    #1
    1. Advertisements

  2. Paul O'Grady

    Rhino Guest

    Have you looked at the Collections trail in the Java Tutorial? It answers at
    least some of your questions and includes some example fragments....

    The URL is http://java.sun.com/docs/books/tutorial/collections/index.html.

    Rhino
     
    Rhino, Feb 15, 2005
    #2
    1. Advertisements

  3. If you are using Java 5.0 you can use...

    ArrayList<Integer> myInts = new ArrayList<Integer>(100);
    myInts.add(3);

    ArrayList<Float> myFloats = new ArrayList<Float>(100);
    myFloats.add(5.0f);

    myInts.size(); //gets the size

    ArrayList part of the java.util.* package, so be sure to import it.
     
    Elliot W. Scott, Feb 16, 2005
    #3
  4. Paul O'Grady

    Paul O'Grady Guest

    Thanks Elliot, that looks very much like C++ templates to me (i.e. terra
    firma). I'm not actually using Java 5.0 though. But supposing I was, how
    would I write a "template" class in java that will allow me to add/
    access and edit items in the ArrayList ?

    Ta (Tks) in advance


    PS: (Questions about Java 5.0

    1). Is Java 5.0 stable ? - i.e. can I use it in a project without being
    too "bleeding edge?
    2). Is Java 5.0 backward compatable / can it coexist with code wriiten
    in a previous version of Java?
     
    Paul O'Grady, Feb 16, 2005
    #4
  5. The ArrayList class is already that template class that you seek.
    Especially with the latest 5.0 generics, it makes it perfect.

    It has add, addAll, get, indexOf, remove, size, etc.

    http://java.sun.com/j2se/1.5.0/docs/api/index.html

    So far so good. I came from .Net last year (yuck) and right now I have
    ran an applet fine under 5.0, an application server under 5.0, and some
    other items. I had one web services failure, but that was obscure. I
    like it.
     
    Elliot W. Scott, Feb 16, 2005
    #5
  6. Paul O'Grady

    Paul O'Grady Guest

    </snip>

    Many thanks for the prompt feedback Elliot - I'll jump to the URL you
    provided, and do a little more reading before deciding what to do ...
     
    Paul O'Grady, Feb 16, 2005
    #6
  7. Paul O'Grady

    John McGrath Guest

    Without knowing more about the original program, it is hard to tell
    whether this is useful, but you could also do this:

    List<Number> numbers = new ArrayList<Number>( SIZE );
    numbers.add( 3 )
    numbers.add( 5.0f );

    Depending on how you want to use them, you may or may not need to cast
    them when you take them out of the array. For example, you could get the
    Number objects and add their floatValues(). If you need to cast them, you
    could do something like this:

    Number n = numbers.get( index );
    if ( n instanceof Integer ) {
    int i = (Integer) numbers.get( 0 );
    } else if ( n instanceof Float ) {
    float f = (Float) numbers.get( 0 );
    } ...
     
    John McGrath, Feb 16, 2005
    #7
  8. Paul O'Grady

    Oscar kind Guest

    Why use casts with generics? IMHO, generics are used best to eliminate
    casts. And with Number instances, this can be done with only a minor
    change:

    Number n = numbers.get(index);
    if (n instanceof Integer) {
    int i = n.intValue();
    } else if (n instanceof Float) {
    float f = n.floatValue();
    }
     
    Oscar kind, Feb 16, 2005
    #8
  9. Paul O'Grady

    John McGrath Guest

    And they often do eliminate the need for casts, but not always.
    Yes, that is better.

    However, the elimination of the need for a cast has nothing to do with
    generics. It is the fact that the Number class knows how to convert to
    multiple primitive types that did it.
     
    John McGrath, Feb 17, 2005
    #9
    1. Advertisements

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.