Hey all,

I'm having some issues using ArrayList. Here's the code..

ArrayList<Double> var = new ArrayList<Double>();
var.add(15.0);
var.add(0.0);

I could just use

double[] var = {15.0 , 0.0};

for this.. but I'm trying to teach myself Java and need to understand the use of ArrayList.

Any insight is appreciated

tons of differences ...
for instance:

double[] var = {15.0, 0.0};

creates an array of two doubles. your length is fixed, so, don't ever try to add a third element.
a List on the other hand will automatically add room for more elements if you add one too many, to say it that way.
that's just one of the differences that can apply to your code. one I assume you also noticed: your array is an array of double (primitive) variables, while the arraylist takes the Double-objects (wrapper). so, another difference: you can make arrays of primitives, but you can't put them in an arraylist, you'll need an actual object for that.

but seriously, comparing the use of arrays against the use of Collections, Lists and Maps is not something to 'learn easily' in a forum post.
you might want to read up on the topics of the what, and then start comparing the benefits.

some points to think about when you compare them:

* sort of element possible
* sorting of the elements
* the speed of the accessibility of elements
* whether or not double values (and I don't mean double variables but non-unique elements) are allowed
...

I'll give you some links you might find usefull:

List

Collections

Map

okay, thanks for the links.

I guess I should have phrased my question differently. (I think) I know enough about the difference between Collections and vectors to know when to use each, but for this application I will need to add X amount of elements to this thing so I do need a Collection. I just can't put a double type using .add() in "var" without getting griped at by my compiler and was curious as to why and the documentation i've read through hasn't led to any fixes. Specifically, I get a "Syntax error on token(s), misplaced construct(s)."

I just compared it to the use of a vector in case someone would recommend that because it is easier.

that's because an array has no add() method. unlike lists and maps (and sets, queues) an array doesn't really come with add(object o), remove(object o), ... methods

you need to place the value like this:
var[2] = 2.0;

...as has been pointed out double is different from Double, double is a primitive type, Double is an immutable object, arraylists and other such collections can only accept objects. If using an array is appropriate for the situation then go ahead and use it, if a dynamic data structure such as an ArrayList is more appropriate then use that.

...as has been pointed out double is different from Double, double is a primitive type, Double is an immutable object, arraylists and other such collections can only accept objects. If using an array is appropriate for the situation then go ahead and use it, if a dynamic data structure such as an ArrayList is more appropriate then use that.

I think what the OP ment was: when is it most appropriate to use arrays and when to use arraylists and such..

I think what the OP ment was: when is it most appropriate to use arrays and when to use arraylists and such..

Not quite accurate. I was wondering why I couldn't use add() without getting a compiler error.

that's because an array has no add() method. unlike lists and maps (and sets, queues) an array doesn't really come with add(object o), remove(object o), ... methods

you need to place the value like this:
var[2] = 2.0;

ArrrayList.add() is what I am trying to use, and I am getting a compiler error.

What is the exact error are you getting ? Can you paste it here ?

ArrayList<Double> var  = new ArrayList<Double>();
var.add(12.0);
var.add(0.0);

Thats the code I am trying to run. The compiler gives me two errors for lines 2 and 3 (these are just the errors for line 2):

1) Syntax error on token "12.0", delete this token <- the '12.0' in var.add(12.0) is highlighted
2) Syntax error on token(s), misplaced construct(s) <- the '.' in var.add(12.0) is highlighted

Which version of Java are you using ? This should run on JDK 1.5 because of autoboxing but since you are using generics you must be using JDK 5. also to isolate the problem just run the above code alone in a main method and let me know the output and whole code, some time error comes on a line but problem resides on previous line or somewhere. if you don't succeed , just paste the whole code .

So apparently it will run in it's own main method.. but I have it in a class being used in the main method:

import static java.lang.System.out;
import java.util.ArrayList;


public class NewMain 
{

    public static void main(String[] args) throws Exception 
    {

        out.println("This is the beginning");
        NewVar initial = new NewVar();
 	
    }
}
import java.util.ArrayList;

public class NewVar 
{
         .
         .
         .

	ArrayList<Double> var  = new ArrayList<Double>();
	var.add(12.0);
	var.add(0.0);

         .
         .
         .

}

and when I try and run this, here is what I get:

This is the beginning
Exception in thread "main" java.lang.Error: Unresolved compilation problems:
Syntax error on token(s), misplaced construct(s)
Syntax error on token "12.0", delete this token
Syntax error on token(s), misplaced construct(s)
Syntax error on token "0.0", delete this token

at NewVar.<init>(NewVar.java:xx)
at NewMain.main(NewMainMain.java:xx)


EDIT: also, I am in JDK 1.6 I believe

you need to put the .add() calls either in a method or in an initialisation block, not "loose" in a class.

look how easy is now to solve your program with full code :) anybody can solve it now. as stultuske suggested put that code for add() method into a method or block.
#
var.add(12.0);
#
var.add(0.0);

awesome, that worked. thanks guys

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.