Using ArrayList (Question)

P

Paul O'Grady

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
 
R

Rhino

Paul O'Grady said:
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
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
 
E

Elliot W. Scott

Paul said:
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

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.
 
P

Paul O'Grady

Elliot said:
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.

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?
 
E

Elliot W. Scott

Paul said:
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 ?

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

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?

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.
 
P

Paul O'Grady

Elliot said:
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.



Yes.
</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 ...
 
J

John McGrath

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);

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 );
} ...
 
O

Oscar kind

John McGrath said:
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 );
} ...

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();
}
 
J

John McGrath

Why use casts with generics? IMHO, generics are used best to eliminate
casts.

And they often do eliminate the need for casts, but not always.
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();
}

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.
 

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. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

Forum statistics

Threads
473,769
Messages
2,569,576
Members
45,054
Latest member
LucyCarper

Latest Threads

Top