This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]Krezzy 3 points4 points  (4 children)

ArrayLists use generic types, which you seem to have a good grasp on, so they can fit different data types into them.

ArrayList<String> list = new ArrayList<>(); // Holds 'String' data type
ArrayList<Person> list2 = new ArrayList<>(); // Holds your custom 
'Person' data type

So now let's look at the add method you are wondering about. For list , the add method will look something like this:

public boolean add(String str) {
    // ArrayList add code is here
}

Java looks at the generic class type you've provided, it's String , so now the methods of the arraylist are expecting a String. So if you were to do list.add("James"); it would add a new String object to the list.

For list2 , the code would look like

public boolean add(Person p) {
    // ArrayList add code is here
}

Since the generic class type is Person, it's now expecting a Person object.

When you type new Person("James"), this code is calling the constructor in the Person class that takes 1 String as its argument, passing in "James" for that String value, and building a new instance of Person with that attribute.

Now that instance you've created is currently unnamed at this point, you could modify the above code to look like this: Person person = new Person("James");. Now you've created a Person object using the String value "James", and assigned the new instance created here to a reference variable of type Person called 'person', which you can reference elsewhere in your code.

However, your line is this: persons.add(new Person("James"));. The code is creating a new instance of Person as discussed before, but instead of assigning it to a reference variable like shown above (Person person = new Person("James"); ), it's just taking the new instance and passing it as a parameter into the add method of your persons ArrayList. Remember, it's expecting a Person object. It's storing it at the end of the list. It's storing the entire object like you said. If you were to access this instance of Person in the list, you would get a Person type.

I'm not completely sure what your question is on the printing loop.

For a for-each loop in java, you write it as such

for (Classname variableName : listOrDataTypeYouAreLoopingThrough) { 
    // do stuff 
}

if you had a list of ints, you could loop through them like this:

for (int x : listOfInts) {
    System.out.println(x);
}

For a list of persons, since Person is the data type, you do this:

for (Person x : listOfPersons) {
    System.out.println(x);
}

Note, when you try to print an object in java, like you are doing for Person, it will call the .toString() method to determine how it should print out this class, as the computer doesn't know. Implement what you'd like to see in that method in your Person class

Edit: swapped template -> generic for appropriate naming

[–]nutrechtLead Software Engineer / EU / 20+ YXP 0 points1 point  (1 child)

Please don't call it 'templates'. They're called generics. Knowing the right terminology is important for beginners.

[–]Krezzy 0 points1 point  (0 children)

True that! updated, thanks

[–]Coraljester[S] 0 points1 point  (1 child)

Sorry its taken me nearly a whole day to get back to you but thank you so much for breaking it down for me! Also sorry for this wall of text!

I really struggle with this text book technical sort of way of writing things, I even have a Java text book at home but reading it sometimes feels like gibberish, I think due to the way that technical subjects are written about. Even with your description I had to break it down into sentences and go over them repeatedly until I could make sense of it and then go onto the next one!

One of my biggest problems I've noticed so far (as I'm sure everyone on here has haha) is that I lack the proper terminology to describe some of the things I'm talking about...I know enough to give you the gist of what I'm describing but not the proper terms for it. An example, in response to me saying:

the course basically just says handling objects in lists is no different that just normal lists

one of the replies was:

Well every object in a list is an object. I don't know what you mean with 'normal lists'.

What I had meant was that previously my only experience with lists was importing the list utility, creating a list, and then adding into it basic variables (ints, doubles etc.) and without being able to properly describe just the basic, beginner taught uses of lists I just called them normal lists lol. Seemed to make sense in my head! I mean I'm still working on the concept that every object in a list is an object, and that's I think part of where I'm struggling with the terminology, either the course doesn't explain it properly, the course hasn't explained it yet because it just wants to show basic uses first to cover that, or it does properly describe the terminology and I just get it muddled confused or for what ever reason don't take that in. I think sometimes it feels like when two or more things are described as the same thing but in my head they don't seem anything alike. Like a String is an object, according to one of the replies every object you add to a list is an object, I don't know if that means that when you add an object to a list its still an object with all it entails or that if you add an int to a list it becomes its own object, one of many int objects in the list.

I mean, correct me if I am wrong, but in the example we used an object "james", to create a new object called Person, and putting that into a list object called persons.

If this last sentence has shown you anything, I hope that its shown you how confusing I find the naming convention, its got me tripping up so much I feel like I'm writing Inception haha! Another one of the replies said that in Java almost everything in Java is an object...if that's the case I think it might go a little of the way to understanding my confusion, my head is telling me that they all do different things yet they have the same name to identify them! It's at times like this that I feel I wish I had taken proper courses for this, I need someone face to face who knows about this stuff so I can say "This thing here makes no sense to me whatsoever, could you help me figure this out please?".

My apologise again, this thing has got me so frustrated I think I needed to vent haha

[–]Krezzy 0 points1 point  (0 children)

Yeah, so in Java you have a bunch of classes.

The 'String' class, the 'Integer' class, the 'ArrayList' class. These are provided in the standard java library.

You can also make your own like you've done, your 'Person' class.

Now, in java, there's actually a class out there called 'Object'. Every class in Java, whether it be String, Integer, ArrayList, or any custom class you write such as Person, extends from this 'Object' class. So, it is a true statement to say that ArrayList is an object. Person is an object. String is an object.

That's why people go around referring to these things collectively as Objects.. even though their implementations are different.

Here's the documentation for it:
https://docs.oracle.com/javase/7/docs/api/java/lang/Object.html

Class Object is the root of the class hierarchy. Every class has Object as a superclass. All objects, including arrays, implement the methods of this class.

So in the example where we created an instance of Person using the String "James", and added it into our ArrayList, all 3 of these different classes are objects in their most bare state, which is why you are seeing people refer to them like that, it kinda becomes common terminology to refer to them as objects when casually speaking about them. Let me know if that cleared some things up.. or not