1

In my hw assignment my professor says to create a data field of type ArrayList. He wants them to be instances of a class. I'm not exactly sure what that means, but my guess is

    ArrayList<CLassName> list = ArrayList<ClassName>();

Can anyone confirm this for me?

Chris
  • 193
  • 1
  • 4
  • 13
  • 3
    I doubt that anyone beside your professor could confirm it, but your guess seems very probable. Anyway you can also improve you code by using `List` interface as reference to `ArrayList`. – Pshemo Apr 01 '14 at 00:46
  • You should confirm this with your professor, because only he/she knows what he/she really means and expects. –  Apr 01 '14 at 00:48
  • @Pshemo `List list = ArrayList();`? What would the difference be between the two? – Chris Apr 01 '14 at 00:49
  • Who is toggling their downvote? Why? – tbodt Apr 01 '14 at 00:50
  • @tbodt It was me, was testing vote animations :) – Pshemo Apr 01 '14 at 00:50
  • @Pshemo april fools? or what? – tbodt Apr 01 '14 at 00:51
  • @Pshemo can you stop please? –  Apr 01 '14 at 00:51
  • @ChrisVachon "What would the difference be between the two" [This](http://stackoverflow.com/questions/383947/what-does-it-mean-to-program-to-an-interface) should answer your question. – Pshemo Apr 01 '14 at 00:53
  • I guess I should have specified a little better. I know what the professor wants, is that the correct syntax? It's seems you are in agreement of that. – Chris Apr 01 '14 at 00:53
  • @Cupcake Just did. There are not so many animations to test. Sorry if that bothered you. – Pshemo Apr 01 '14 at 00:54
  • @Pshemo That is way too much for me to read right now, so I will trust you on changing to "List" instead of "ArrayList" and read that sometime tomorrow when I'm not in a hurry. Thank you, though. – Chris Apr 01 '14 at 00:56

3 Answers3

3

You can use something like this:

List<String> list = new ArrayList<String>();

Since Java SE 1.7, it can a little simpler:

List<String> list = new ArrayList<>();
sendon1982
  • 9,982
  • 61
  • 44
0

Yes.

NOTE: The rest is background knowledge about ArrayLists.

Let's say you want an ArrayList of Strings. Keep in mind Strings are Objects.

// Creates ArrayList
ArrayList<String> list = ArrayList<String>();

// Adds elements to ArrayList
list.add("Hello");
list.add("world!");

// Iterate through ArrayList
for (String str : list) {
    // Print the String in the list.
    System.out.print(str + " ");
}
// Print newline character.
System.out.print("\n");

The for (String str : list) is a for each loop which allows you to iterate through each element in the list.

Anubian Noob
  • 13,426
  • 6
  • 53
  • 75
  • That doesn't answer the question. – tbodt Apr 01 '14 at 00:49
  • Now it does, but it has a lot of redundant content. – tbodt Apr 01 '14 at 00:50
  • @tbodt I need to get into the habit of reading the questions before answering, but what about now. – Anubian Noob Apr 01 '14 at 00:50
  • I think that the OP is learning about how to use `ArrayList`s in his class. – tbodt Apr 01 '14 at 00:52
  • Well that's why I includede the information, but yeah I could have answered the question the first time around... – Anubian Noob Apr 01 '14 at 00:53
  • 1
    "Let's say you want an ArrayList of Strings. Keep in mind Strings are Objects." That is literally all I needed to see to confirm what I was looking for. I always forget that Strings are objects because my first programming class they taught us that they were primitives. And that was my belief for about a year, so somtimes it just slips my mind. – Chris Apr 01 '14 at 01:04
  • @ChrisVachon in some languages they are primatives, and they are declared literally and automatically translated to a String object so they can be misleading. In addition, the java compiler accepts adding for concatanation, which can be misleading. Glad I could help though :) – Anubian Noob Apr 01 '14 at 01:07
0

Yes, that's right. I do, however, recommend this instead:

List<ClassName> list = new ArrayList<ClassName>();

That way you can change the type of list in just one place instead of two.

tbodt
  • 16,609
  • 6
  • 58
  • 83