File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes What is the starting index of a LinkedList Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "What is the starting index of a LinkedList" Watch "What is the starting index of a LinkedList" New topic
Author

What is the starting index of a LinkedList

Charles Sexton
Ranch Hand

Joined: Sep 26, 2013
Posts: 193
what is the starting index for linked list, 0 or 1. I know an array starts at 0 so wouldn't a linked list?
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18896
    
  40

Charles Sexton wrote:what is the starting index for linked list, 0 or 1. I know an array starts at 0 so wouldn't a linked list?


Well, what does the JavaDoc say? ... http://docs.oracle.com/javase/7/docs/api/java/util/List.html#get%28int%29

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Charles Sexton
Ranch Hand

Joined: Sep 26, 2013
Posts: 193
Henry Wong wrote:
Charles Sexton wrote:what is the starting index for linked list, 0 or 1. I know an array starts at 0 so wouldn't a linked list?


Well, what does the JavaDoc say? ... http://docs.oracle.com/javase/7/docs/api/java/util/List.html#get%28int%29

Henry


0....+1 for java api reference...."durdur" from madea
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 8008
    
  22

Charles Sexton wrote:0....+1 for java api reference...."durdur" from madea

??? You'll have to explain that.

There is only ONE correct answer; and you could find it out pretty easily yourself by testing.

Winston

Isn't it funny how there's always time and money enough to do it WRONG?
Articles by Winston can be found here
Charles Sexton
Ranch Hand

Joined: Sep 26, 2013
Posts: 193
Winston Gutkowski wrote:
Charles Sexton wrote:0....+1 for java api reference...."durdur" from madea

??? You'll have to explain that.

There is only ONE correct answer; and you could find it out pretty easily yourself by testing.

Winston


.get(0) is the first index of a linked list used by the java api. However a person may create their own linked list and start the index at 1.....
Charles Sexton
Ranch Hand

Joined: Sep 26, 2013
Posts: 193
Charles Sexton wrote:
Winston Gutkowski wrote:
Charles Sexton wrote:0....+1 for java api reference...."durdur" from madea

??? You'll have to explain that.

There is only ONE correct answer; and you could find it out pretty easily yourself by testing.

Winston


.get(0) is the first index of a linked list used by the java api. However a person may create their own linked list and start the index at 1.....You are right I should've just tested it according to the linked list java api.....very easily to add one element and to get that element.....
Charles Sexton
Ranch Hand

Joined: Sep 26, 2013
Posts: 193
Charles Sexton wrote:??? You'll have to explain that.

There is only ONE correct answer; and you could find it out pretty easily yourself by testing.

Winston


.get(0) is the first index of a linked list used by the java api. However a person may create their own linked list and start the index at 1.....You are right I should've just tested it according to the linked list java api.....very easily to add one element and to get that element.....
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18896
    
  40

Charles Sexton wrote:
.get(0) is the first index of a linked list used by the java api. However a person may create their own linked list and start the index at 1.....You are right I should've just tested it according to the linked list java api.....very easily to add one element and to get that element.....


Perhaps, but it would not be compliant with the java.util.List interface... So, this linked list would not be a java.util.List.

Henry
Charles Sexton
Ranch Hand

Joined: Sep 26, 2013
Posts: 193
Henry Wong wrote:
Charles Sexton wrote:
.get(0) is the first index of a linked list used by the java api. However a person may create their own linked list and start the index at 1.....You are right I should've just tested it according to the linked list java api.....very easily to add one element and to get that element.....


Perhaps, but it would not be compliant with the java.util.List interface... So, this linked list would not be a java.util.List.

Henry


You are right Henry. You would also need to create your own methods to add, remove and get elements. Also if you needed a toArray or toString method....their are other methods but these are the most useful....
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 8008
    
  22

Charles Sexton wrote:You are right Henry. You would also need to create your own methods to add, remove and get elements.

As it stands at the moment, you're correct; but one could imagine an AbstractList implementation that had a
protected int convertIndex(int i) { ...
method for which that wasn't the case.

It's possibly also worth pointing out that List.subList() is often implemented via an internal class that does convert the supplied index to an offset - but not for a LinkedList, I suspect.

Winston
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: What is the starting index of a LinkedList