-4

I just started with lists in Java and I'm confused with all this. So basically we have ArrayList and LinkedList which can be defined like

    LinkedList<String> s = new LinkedList<String>();

    ArrayList<String> s = new ArrayList<String>();

But then we also have LinkedIntList which can be defined like for example:

class LinkedIntList {
    private ListNode first;
    private int size;

    LinkedIntList () {
        first=null;
        size=0;
    }

    LinkedIntList(LinkedIntList l) {
        first = l.getFirst();
        size=l.size();
    }

    ListNode getFirst() {return first;}
        int size() {return size;}
    }

But this is my problem, why define LinkedIntList using a class? What is the difference comparing to LinkedLists where we just define like I stated. I can't understand why a class is being used. If this is a 'new type' of array why using a class instead of declaring it normally?

Sorry if this sounds weird, but I'm a beginner and really need help at this.

Thank you!

Richard Tingle
  • 16,906
  • 5
  • 52
  • 77
Pedro B
  • 61
  • 1
  • 6
  • Because a `LinkedList` still exists in java so you can instantiate this class, but there is no `LinkedIntList` in java implemented (I have never heared of a `LinkedIntList` from where did you got this code?) so you can creat this class by your own. *Note: List is only an interface. – kai May 14 '14 at 10:10

6 Answers6

0

A List in java, is an ordered collection. I suppose you are a beginner and confused with syntax. If you have java decompiler, you can see that LinkedList and ArrayList are also classes. Which means somewhere some good person has done the coding for you and provided you a class which is similar to your "LinkedIntList" and provided you out of the box. You don't need to create a new class if java provides sufficient functionality for you.

But sometimes, the out of the box classes are not sufficient for our requirements. In that case we have to write our own implementation of classes, such as your class "LinkedIntList".

In this case, it seems you need size and one element hence you are creating it on your own.

Mohit Kanwar
  • 2,962
  • 7
  • 39
  • 59
  • So does the same happen when we declare a new array? – Pedro B May 14 '14 at 10:35
  • Actually, internally array is also a class in Java please refer [this](http://stackoverflow.com/questions/8546500/why-isnt-there-a-java-lang-array-class-if-a-java-array-is-an-object-shouldnt) – Mohit Kanwar May 14 '14 at 10:43
0

LinkedList is a class defined in the java.util package, and it's already provided to you. It exploits a feature called Generics that allows you to provide the type of objects that will populate the list. You basically take it as a black box: in most cases, you don't care about LinkedList actual implementation, but only about its interface (that is, the methods it exposes to you).

The class LinkedIntList that you provided does not feature genericity, but is simply an implementation of a linked list where each item is a ListNode. I guess a ListNode contains an integer, otherwise the name LinkedIntList wouldn't make much sense.

Anyway, you could discard this implementation (although is good for learning) and simply declare

LinkedList<Integer> myLinkedList = new LinkedList<Integer>();

If you want to know more about generics, take a look at the java tutorials.

link
  • 1,676
  • 1
  • 13
  • 21
0

In your first example,

LinkedList<String> s = new LinkedList<String>();

it is the implementation of LinkedList defined in the stardard library, specifically from package java.util. See here: http://docs.oracle.com/javase/7/docs/api/java/util/LinkedList.html

You can look for its source code (1000+ lines long so I won't be posting it here) and compare it to your custom implementation of LinkedIntList.

Basically, Java already provided a default implementation of LinkedList(your first example) but everyone can still write(a class) and use their own implementation (your second example).

renz
  • 1,072
  • 3
  • 11
  • 21
0

You're question isn't clear, but I'll try to explain to you what Lists are essentially.

Lists are a type of data structure, not only in Java but in a lot of programming language. Arrays are also a type of data structure. Data structures hold and manage data in an organized manner.

In Java, the main difference between a List and an Array is that a List has dynamic size, while an Array is of fixed size set when the Array was declared.

All Lists in Java are implementation of the List interface. If you don't know what interfaces are, I suggest you learn about it, but basically it means that all Lists can do the same basic set of things for you, but do them differently internally.

For example, ArrayList uses Arrays internally in order to expand or diminish the List as necessary (implement the dynamic size). LinkedList implements things differently internally, using nodes that are connected to each other. But they both offer the same basic sets of operations to the programmer, defined in the List interface (although one may offer additional methods the other doesn't).

Because the differ in their inner implementation, they might differ in performance for different operations. They have different algorithms to do things (for example, access a value in an index), with different 'speeds' (complexities) of doing so.

Most of the time ArrayList will be the right, simpler choice. This answer talks about when to prefer one over the other.

Hope this helps.

Community
  • 1
  • 1
Aviv Cohn
  • 15,543
  • 25
  • 68
  • 131
0

A java List is an ordered collection of objects, this java.util.List is actually an interface (contract) that defined what a list should behave like.

There are multiple variations (implementations) of List in the java standard JDK, while each is different from the other, they honor the contract defined in List (the ordered aspect for example). These implementations are concrete classes and you choose from in your code.

LinkedList and ArrayList you mentioned are JDK implementations of List.

The class you shared (LinkedIntList) is a custom made structure of objects, that neither part of the JDK, nor implement the java List interface.

Ahmad Y. Saleh
  • 3,309
  • 7
  • 32
  • 43
-1

LinkedList is a part of Java platform class in java.util package and it is widely used in different tasks. And it supports Generics .

LinkedIntList is a custom implementation just to show you how single-linked list coudl be implemented.

You can find more about different algorithms and structures and also complexity here

Roman Bondar
  • 82
  • 1
  • 6