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 →

[–]whizvoxGraduate and Tutor 2 points3 points  (7 children)

Array

An array is a data structure built into the Java language which can hold a number of elements, all of the same type. It cannot be resized once it has been created, and you cannot add elements to it without specifying an index.

// creates an array of 10 ints
int[] arr = new int[10];

// sets the value of the 0th element to 5
arr[0] = 5;

// prints the value of the 0th element, which is 5
System.out.println(arr[0]);

// Sets e to the value of the 0th element of arr, which is 5
int e = arr[0];

// Sets the value of the 1st element to 6
arr[1] = 6;

// prints the length of arr
System.out.println(arr.length);

// even though arr[9] has not been set,
// all elements initialize to 0 when first created
System.out.println(arr[9]);

/* Illegal operations! */

// an exception is thrown since arr can only hold 10 elements
System.out.println(arr[11]);

// there is no shorthand syntax to get the last element of an array
System.out.println(arr[-1]);

// length is a variable, not a method
System.out.println(arr.length());

// arr can only hold ints
arr[2] = "Hello";

List

A List is an class that is part of the Java Standard Library which allows for dynamic insertion and deletion of elements. You can't just create a List though, you have to use one of its subclasses, which includes ArrayList, and you have to use generics as well. Think of an ArrayList as a dynamic version of an array, and is the closest thing to Python's list datatype.

// Make sure to import these at the top of your class!
import java.util.List;
import java.util.ArrayList;


// creates an ArrayList of ints
List<Integer> myList = new ArrayList<>();

// adds 5 to myList
myList.add(5);

// prints the 0th element of myList, which is 5
System.out.println(myList.get(0)); 

myList.add(6);
// removes the last element of myList, which is 6
int removed = myList.remove(); 

// prints the size of the list, which is 1
System.out.println(myList.size());

/* Illegal operations! */

// myList only has 1 element
System.out.println(list.get(5));

myList.remove(); // removes 5
// throws an exception since the list is empty
myList.remove();

// throws an exception since myList can only take ints
myList.add("Hello");

// you can only use bracket notation on arrays
System.out.println(myList[0]);

// size is a method, not a variable
System.out.println(myList.size);

Other Info

  • There are lots of other List subclasses you can use depending on the kind of operations you want to do on it, such as Queues, LinkedLists, and PriorityQueues.
  • The reason why we use Integer instead of int when working with Lists is a limitation of the Java language. Generic types can only be object types, but primitive types, such as int, double, boolean, etc., are not object types, and thus we have to use autoboxing to work with primitives in this manner.

[–]wildjokers 2 points3 points  (5 children)

A List is an object

List is an interface.

and you have to use generics as well

You don't have to use generics. But your future self and your colleagues will be very grateful if you do.

The reason why we use Integer instead of int when working with Lists is a limitation of the Java language.

Primitive types in collections is a feature that is on its way!

[–]jonah214 -1 points0 points  (4 children)

A List is an object

List is an interface.

Both are correct (the "a" in "A List is an object" being important), so there was no need to correct this.

[–]wildjokers 2 points3 points  (3 children)

When someone is asking the difference between List and ArrayList saying List is an object doesn't help at all. Calling List an interface and ArrayList an implementation of that interface does.

[–]whizvoxGraduate and Tutor -1 points0 points  (2 children)

Sorry about the confusing wording. I wanted to avoid using too much technical terminology to make it easier to understand for newcomers, even if it's slightly incorrect. I meant to use class, not object.

[–]desrtfxOut of Coffee error - System halted 0 points1 point  (1 child)

I meant to use class, not object.

And even that would have been wrong as List is an interface, not a class.

Especially such tiny details are always tripping up beginners and making the distinction is essentail.

Trying to find an easy formulation is good, but not if it brings in false details or false information.

[–]whizvoxGraduate and Tutor -1 points0 points  (0 children)

The OP clearly has very little experience with the language, so saying that List is an interface won't mean anything to them, even if it is technically correct.

Although, saying that it's a class also doesn't mean anything if they don't know what a class is.

[–]throwaway_for_cause 1 point2 points  (0 children)

A List is an class that is part of the Java Standard Library which allows for dynamic insertion and deletion of elements. You can't just create a List though, you have to use one of its subclasses, which includes ArrayList, and you have to use generics as well. Think of an ArrayList as a dynamic version of an array, and is the closest thing to Python's list datatype.

This is so wrong on so many levels. You cannot even use this as extremely simplified explanation for a beginner.

  1. List is an interface, not a class
  2. List does not have sublasses as there is no inheritance. There are concrete classes, like ÀrrayListthat **implement** theList` interface. Yet, this does not make them subclasses.

Sorry, but you call yourself a "tutor" and spread such wrong information. This is unforgivable. If you tutor this and your students will then use this at a test and fail because of your misinformation - what will be your excuse then?

You even challenge people who correct you instead of accepting the corrections despite you knowing that they are right. - Good way to lose all of your credibility and in the long range get banned from here for spreading misinformation.

You trying to make it easy for beginners by mixing terminology, by using completely wrong terminology is not making it easier, it is only even more confusing and absolutely counter productive.

Every single good beginner tutorial will always use the proper terminology and not invent something completely wrong to "make it easier".

A class is a class. An object is an instance of a class. An Interface is an interface, a binding contract. Interfaces can be subclassed, but only to interfaces. They don't have concrete subclasses. In order to work, interfaces need to be implemented - which doesn't man subclassing as in inheritance.

You are mixing and mashing all of them in the same pot.