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?

asked Apr 1, 2014 at 0:44
11
  • 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. Commented Apr 1, 2014 at 0:46
  • You should confirm this with your professor, because only he/she knows what he/she really means and expects. Commented Apr 1, 2014 at 0:48
  • @Pshemo List<CLassName> list = ArrayList<ClassName>();? What would the difference be between the two? Commented Apr 1, 2014 at 0:49
  • Who is toggling their downvote? Why? Commented Apr 1, 2014 at 0:50
  • @tbodt It was me, was testing vote animations :) Commented Apr 1, 2014 at 0:50

3 Answers 3

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<>();
answered Apr 1, 2014 at 0:56
Sign up to request clarification or add additional context in comments.

Comments

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.

answered Apr 1, 2014 at 0:48

7 Comments

That doesn't answer the question.
Now it does, but it has a lot of redundant content.
@tbodt I need to get into the habit of reading the questions before answering, but what about now.
I think that the OP is learning about how to use ArrayLists in his class.
"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.
|
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.

answered Apr 1, 2014 at 0:48

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.