Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Agile forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Passing ArrayList

 
R Jarman
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a class with a private variable

privateArrayList<Bid>bids = new ArrayList<Bid>();

I'm setting this variable in the constructor for the class, so the constructor looks like this.

public Pruning ( ArrayList _bids)
{
bids.addAll ( _bids );
}

I don't think I'm doing something right. How do I tell the constructor the type of the argument. I tried

public Pruning ( ArrayList<Bid> _bids)

like I did in the variable declaration but it won't compile. What am I missing?
 
Garrett Rowe
Ranch Hand
Posts: 1296
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I tried

public Pruning ( ArrayList<Bid> _bids)

like I did in the variable declaration but it won't compile. What am I missing?


That looks okay. What type of error message did the compiler give?
 
Robert Hill
Ranch Hand
Posts: 94
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How are you invoking the constructor?

ie Pruning p = new Pruning(bids)

Why do you instantiating an ArrayList as a private member and then passing another one in the constructor. Is that what you want?

This might be what you want:

private ArrayList<Bid> bids;

public Pruning ( ArrayList<Bid> _bids)
{
bids = _bids;
}


addAll() appends the passed list to the private one, but the private list has no elements yet.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic