0

I've tried comparing this to examples and I just can't seem to find out why this will not compile so I was hoping for some insight.

ArrayList<Integer> listOfPrimeNumbers(initialCapacity) = new ArrayList<Integer>( );

is my code. My understanding is that its creating a new ArrayList object, of type Integer which is called listOfPrimeNumbers and has an initial capacity of the argument I am passing in which is called initialCapacity.

Unfortunately I get a "';' expected" error when I try and compile this ? Could someone please help ?

4
  • 3
    I can't believe all the upvotes on this posting. duffymo had the proper answer and was first to respond .All the other posting where done minutes later. Upvoting duplicate answers just encourages multiple unnecesary postings. There is nothing to be added to this posting. It was a simple and straight forward question with an equally simple straight forward answer. Commented Nov 28, 2010 at 16:26
  • @camickr: Right on. And thank you for deleting your own duplicate answer as soon as you saw duffymo had already gotten there. I wish more people would do it. Commented Nov 28, 2010 at 16:44
  • 1
    I think I voted them up. They were correct, of course. Is that wrong? Commented Nov 28, 2010 at 22:30
  • @camickr: They were 1 minutes later, so likely written at the same time, and even if they're redundant, the effort can be rewarded with an upvote. It's not a race. Commented Jul 28, 2011 at 21:48

3 Answers 3

10

This is correct:

ArrayList<Integer> listOfPrimeNumbers = new ArrayList<Integer>(initialCapacity);

Do you see why? The first bit declares the static type of the reference; the second bit is the name of the reference; the third calls the constructor and initializes the memory.

I might recommend that you think about coding it this way:

List<Integer> listOfPrimeNumbers = new ArrayList<Integer>(initialCapacity);
Sign up to request clarification or add additional context in comments.

1 Comment

Not an idiot - no need for using such words on yourself. We all get code blind at times.
3

You are trying to pass an argument to the NAME of the variable. It should go in the constructor on the right side of the = sign

ArrayList<Integer> listOfPrimeNumbers = new ArrayList<Integer>(100);

Comments

1
ArrayList<Integer> listOfPrimeNumbers = new ArrayList<Integer>(initialCapacity);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.