2

I just wrote this whole program using a bunch of array of classes. One for example is:

private Cat casts[] = new Cat[numOfCats]; 

and was recently informed by my teacher that we need to be using ArrayList.

Now the assignment is almost due and I'm looking for a quick way to resolve this. From what I have read online I would create the list like this:

ArrayList<Cat> cats = new ArrayList<Cat>(numOfCats);

What I am wondering is how do I access setters and getters of the new ArrayList cats? From what I can find it would be something like cats get(i).getLocation().x; or cats get(i).setSize(100); but neither of those are working.

Thank you for your time and I hope to hear back from you shortly!

3
  • Please show your code and how these are not working. Unfortunately my crystal ball is out of order today. Commented Sep 24, 2015 at 22:19
  • 2
    Looks like you may be missing a . -- try using cats.get(i) to get the Cat instance at position i in the collection (rather that cats get(i)). Commented Sep 24, 2015 at 22:20
  • Yes, you were right, neuronaut. I was following a guide online and they didn't have a . there so I didn't know I need one. Thanks! Commented Sep 25, 2015 at 23:56

3 Answers 3

2

ArrayList is a wrapper class for dynamically resizing array. While plain array provides only "setter", "getter" and "length" methods, ArrayList provides this and much more.

Suppose, you have Cat [] catArray and List<Cat> catList. (See below, why List and not ArrayList).

  1. Setter: instead of catArray[i] = someCat use catList.set(i, someCat)
  2. Getter: instead of someCat = catArray[i] use someCat = catList.get(i). Getter returns Cat, so you can call methods on returned object as this: catList.get(i).meow();
  3. Size: catArray.length becomes catList.size()

List also provides such methods as add (insert element at position or append to the end of the list), remove (delete element from list), indexOf to search element, etc.

The biggest advantage of ArrayList comparing to plain array is that it is resizable. You don't need to specify fixed size in advance, ArrayList will grow as you add elements and shrink as you remove them.

This resizing nature of List also implies the main difference between these two structures. When working with Arrays, you used to create array of fixed length and then set it's elements one by one:

Cat[] cats = new Cat[10];
for (int i = 0; i < cats.length; i ++) {
   cats[i] = new Cat(); // populate cat #i
}

With lists you usually create empty list and then add elements to it. You don't need to care about size, as list grows as you add elements:

List<Cat> cats = new ArrayList<Cat>();
// we want to add 10 cats
for (int i = 0; i < 10; i ++) {
   cats.add(new Cat()); //add new cat to the end of the list
}

Regarding the declaration List<Cat> cats = new ArrayList<Cat>();. In java it is considered good practice to declare variable as the least concrete interface as practical. For lists it's usually Collection or List. However it will not be a mortal mistake if you declare your cats as ArrayList<Cat> cats = new ArrayList<Cat>();

I intentionally omitted many aspects of Arrays vs Lists, including primitives, performance, generics. Internet is full of articles that can provide more details if OP is interested.

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you! Yeah one of the big parts was that I was not putting the . But your other things listed helped a lot as well. Oh and thanks for making my post look better haha.
0

To add an element to the ArrayList use the method add, for example: cats.add(new Cat()); and to get an element use the method get, for example cats.get(index); where index is the position of the Cat object in the ArrayList.

Comments

0
Arraylist<String> cats = new Arraylist();//using string for easier understanding

cats.get (i);//will get the string object at index i
cats.add ("hi");
cats.add (new String ("world!");//will add a new string to the list. If your using an arraylist note that it will not be sorted in any way

That's how you get indexes from an arraylist.

Just as a side note: use java.util.List rather than arraylist as you can change the type later.

2 Comments

This suggestions has a bit of code smell to it. It's not a good idea to create an ArrayList instance without a type parameter (i.e. ArrayList<Cat> is preferable). And suggesting to put different object types such as Cat and String into the same collection is just asking for trouble.
Im so sorry. Having issues with my phone. Will fix it now

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.