java.lang.Object
java.util.AbstractCollection<E>
java.util.AbstractList<E>
java.util.AbstractSequentialList<E>
java.util.LinkedList<E>
- All Implemented Interfaces:
- Serializable, Cloneable, Iterable<E>, Collection<E>, List<E>, Queue<E>
- See Also:
- Top Examples, Source Code,
ArrayList
,
Vector
,
Collections.synchronizedList(List)
public boolean add(E o)
- See Also:
- AbstractList, List, Collection
- Geek's Notes:
- Description Add your codes or notes Search More Java Examples
[1939]Student College programme
By swords { dot } business { at } hotmail { dot } com on 2007/11/06 12:38:01 Rate
//
//This method returns the next student object in the List
//
public void findNext ( Student input )
{
< Dont know what to put here, i want to return the next student object in the list
}
//HERE IS THE REST OF THE CLASS
import java.util.*;
public class StudentLinkedList {
private int count;
private StudentNode list;
private StudentNode last;
public StudentLinkedList ( )
{
count = 0;
last = null; // this points to the last student in the list
list = null; // this points to the first student in the list
}
//
//Add the specified student to the end of the list
//if its not already present
//
public void add ( Student element )
{
if ( ! ( contains ( element ) ) )
{
StudentNode node = new StudentNode ( element ) ;
node.setNext ( list ) ;
list = node;
if ( size ( ) == 0 )
last = node;
count++;
}
} //end add
//
//Remove the specified student from the list
//
public Student remove ( Student target )
{
boolean found = false;
StudentNode previous, current;
Student result = null;
if ( isEmpty ( ) )
System.out.println ( "The List is Empty" ) ;
if ( list.getStudent ( ) .equals ( target ) )
{ //note that the "equals" above needs to be overwritten
//in whatever class is going to use this LinkedSet
result = list.getStudent ( ) ;
list = list.getNext ( ) ;
}
else
{
previous = list;
current = list.getNext ( ) ;
for ( int look=0; look < count - 1 && !found; look++ )
if ( current.getStudent ( ) .equals ( target ) )
found = true;
else
{
previous = current;
current = current.getNext ( ) ;
}
if ( !found )
throw new NoSuchElementException ( ) ;
result = current.getStudent ( ) ;
previous.setNext ( current.getNext ( ) ) ;
count--;
}
return result;
}
//
// This method checks if the List is empty and
// returns a boolean value to indicate the answer
//
public boolean isEmpty ( )
{
return ( size ( ) == 0 ) ;
}
public int size ( )
{
return count;
}
//
//This method prints a string representation of the list
//
public String toString ( )
{
String result = "";
StudentNode current = list;
while ( current != null )
{
result += current.getStudent ( ) .toString ( ) + "\n";
current = current.getNext ( ) ;
}
return result;
}
//
// Returns true if this set contains the specified target
// element.
//
public boolean contains ( Student target )
{
boolean found = false;
StudentNode current = list;
for ( int look=0; look < count && !found; look++ )
if ( current.getStudent ( ) .equals ( target ) )
found = true;
else
current = current.getNext ( ) ;
return found;
}
//
//This method returns the first student object in the List
//
public Student findFirst ( )
{
return list.getStudent ( ) ;
}
//
//This method returns the last student object in the list
//
public Student findLast ( )
{
return last.getStudent ( ) ;
}
public void add(int index,
E element)
- See Also:
- IndexOutOfBoundsException, AbstractSequentialList, List
- Geek's Notes:
- Description Add your codes or notes Search More Java Examples
public void add(int index,
Object element)
- See Also:
- IndexOutOfBoundsException, AbstractSequentialList, List
- Geek's Notes:
- Description Add your codes or notes Search More Java Examples
[562]Place elements anywhere in the current list
By KansasT on 2004/11/29 07:44:25 Rate
The add with an index allows you to place elements anywhere in the current list, as the example will show.
public static void main ( String [ ] args ) throws Exception
{
LinkedList list = new LinkedList ( ) ;
list.add ( "Hello" ) ;
list.add ( "world" ) ;
list.add ( 0,"there" ) ;
list.add ( 0,"there1" ) ;
list.add ( 1,"here" ) ;
ListIterator itt = list.listIterator ( ) ;
while ( itt.hasNext ( ) )
{
String line = ( String ) itt.next ( ) ;
System.out.println ( line ) ;
}
} // end main
The output of this is:
there1
here
there
Hello
world
since to start with we have:
- > "Hello"- > "world"- > end
1 2
After the add ( 0,"there" )
we have
- > "there"- > "Hello"- > "world"- > end
1 2 3
And after add ( 0,"there1" ) we have
- > "there1"- > "there"- > "Hello"- > "world"- > end
1 2 3 4
therefore when you do the add ( 1,"here" )
you get:
- > "there1"- > "here"- > "there"- > "Hello"- > "World"- > end
1 2 3 4 5
because it will insert "here" after element 1 which is now "there1".
[1864]LinkedList with runtime input
By Mubin Ahmed on 2007/02/02 09:18:36 Rate
import java.util.Scanner;
import java.util.*;
public class LinkedListUtil {
public static void main ( String args [ ] ) {
Scanner in=new Scanner ( System.in ) ;
LinkedList list=new LinkedList ( ) ;
while ( in.hasNext ( ) ) {
String word = in.next ( ) ;
list.add ( word ) ;
}
ListIterator itt = list.listIterator ( ) ;
while ( itt.hasNext ( ) )
{
String line = ( String ) itt.next ( ) ;
System.out.println ( line ) ;
}
System.out.println ( "\nList in reverse order........." ) ;
while ( itt.hasPrevious ( ) )
{
String line = ( String ) itt.previous ( ) ;
System.out.println ( line ) ;
}
}
}
public boolean add(Object o)
- See Also:
- AbstractList, List
- Geek's Notes:
- Description Add your codes or notes Search More Java Examples
[940]LinkedList
By parisjackson { at } yahoo { dot } com on 2004/09/27 17:52:07 Rate
LinkedList list = new LinkedList ( ) ;
Integer i1 = new Integer ( 1 ) ;
Integer i2 = new Integer ( 2 ) ;
list.add ( i1 ) ;
list.add ( i2 ) ;
//now we have a list that looks like
// - > 1- > 2
public boolean addAll(int index,
Collection<? extends E> c)
- See Also:
- NullPointerException, IndexOutOfBoundsException, AbstractSequentialList, List
- Geek's Notes:
- Description Add your codes or notes Search More Java Examples
[941]Nested LinkedList
By parisjackson { at } yahoo { dot } com on 2004/09/27 17:55:55 Rate
LinkedList list1 = new LinkedList ( ) ;
LinkedList list2 = new LinkedList ( ) ;
Integer i1 = new Integer ( 1 ) ;
Integer i2 = new Integer ( 2 ) ;
Integer i3 = new Integer ( 3 ) ;
list1.add ( i1 ) ;
list1.add ( i2 ) ;
list1.add ( i3 ) ;
list2.add ( "Shave" ) ;
list2.add ( "And" ) ;
list2.add ( "A" ) ;
list2.add ( "Haircut" ) ;
list2.add ( "Two" ) ;
list2.add ( "Bits" ) ;
//Now we list1 that looks like
// - > 1- > 2- > 3
//And list2 looks like
//- > "Shave"- > "And"- > "A"- > "Haircut"- > "Two"- > "Bits"
list2.add ( 2, list1 ) ;
//Now list2 looks like
//- > "Shave"- > "And"- > [ 1- > 2- > 3 ] - > "A"- > "Haircut"- > "Two"- > "Bits"
public boolean addAll(Collection<? extends E> c)
- See Also:
AbstractCollection.add(Object)
, NullPointerException, List
- Geek's Notes:
- Description Add your codes or notes Search More Java Examples
[942]_
By parisjackson { at } yahoo { dot } com on 2004/09/27 17:57:34 Rate
LinkedList list1 = new LinkedList ( ) ;
LinkedList list2 = new LinkedList ( ) ;
Integer i1 = new Integer ( 1 ) ;
Integer i2 = new Integer ( 2 ) ;
list1.add ( i1 ) ;
list1.add ( i2 ) ;
list1.add ( i3 ) ;
list2.add ( "Shave" ) ;
list2.add ( "And" ) ;
list2.add ( "A" ) ;
list2.add ( "Haircut" ) ;
list2.add ( "Two" ) ;
list2.add ( "Bits" ) ;
//Now we list1 that looks like
// - > 1- > 2- > 3
//And list2 looks like
//- > "Shave"- > "And"- > "A"- > "Haircut"- > "Two"- > "Bits"
list2.add ( list1 ) ;
//Now list2 looks like
//- > "Shave"- > "And"- > "A"- > "Haircut"- > "Two"- > "Bits"- > [ 1- > 2- > 3 ]
public void addFirst(E o)
- Geek's Notes:
- Description Add your codes or notes Search More Java Examples
public void addFirst(Object o)
- Geek's Notes:
- Description Add your codes or notes Search More Java Examples
public void addLast(E o)
- Geek's Notes:
- Description Add your codes or notes Search More Java Examples
public void addLast(Object o)
- Geek's Notes:
- Description Add your codes or notes Search More Java Examples
public void clear()
- See Also:
- E, AbstractList, List, Collection
- Geek's Notes:
- Description Add your codes or notes Search More Java Examples
[943]Clear LinkedList
By parisjackson { at } yahoo { dot } com on 2004/09/27 18:00:04 Rate
LinkedList list = new LinkedList ( ) ;
Integer i1 = new Integer ( 1 ) ;
Integer i2 = new Integer ( 2 ) ;
Integer i3 = new Integer ( 3 ) ;
list1.add ( i1 ) ;
list1.add ( i2 ) ;
list1.add ( i3 ) ;
//Now we list1 that looks like
// - > 1- > 2- > 3
list.clear ( ) ;
//Now the list is empty
int i = list.size ( ) ; //i = 0
public Object clone()
- See Also:
Cloneable
- Geek's Notes:
- Description Add your codes or notes Search More Java Examples
public boolean contains(Object o)
- See Also:
- E, AbstractCollection, List, Collection
- Geek's Notes:
- Description Add your codes or notes Search More Java Examples
[944]Check to see if an item in a LinkedList
By parisjackson { at } yahoo { dot } com on 2005/07/20 14:40:47 Rate
ist.add ( "Shave" ) ;
list.add ( "And" ) ;
list.add ( "A" ) ;
list.add ( "Haircut" ) ;
list.add ( "Two" ) ;
list.add ( "Bits" ) ;
//Now list looks like
//- > "Shave"- > "And"- > "A"- > "Haircut"- > "Two"- > "Bits"
boolean b = list.contains ( "Haircut" ) ; //b = true
public E element()
- See Also:
- NoSuchElementException, Queue
- Geek's Notes:
- Description Add your codes or notes Search More Java Examples
public E get(int index)
- See Also:
- IndexOutOfBoundsException, AbstractSequentialList, List
- Geek's Notes:
- Description Add your codes or notes Search More Java Examples
[945]Retrieve item from LinkedList
By parisjackson { at } yahoo { dot } com on 2004/09/27 18:05:46 Rate
LinkedList list = new LinkedList ( ) ;
list.add ( "Shave" ) ;
list.add ( "And" ) ;
list.add ( "A" ) ;
list.add ( "Haircut" ) ;
list.add ( "Two" ) ;
list.add ( "Bits" ) ;
//Now list looks like
//- > "Shave"- > "And"- > "A"- > "Haircut"- > "Two"- > "Bits"
String s1 = ( String ) list.get ( 1 ) ; //s1 = "And"
String s2 = ( String ) list.get ( 3 ) ; //s1 = "Haircut"
[1307]_
By pillepop2003 at yahoo { dot } de on 2005/02/16 14:04:24 Rate
Don't forget to ( typecast ) the Object returned bet get ( int ) -
as I'm quite a newbie to java, it costed me 4958839 hours to find out.
bigup 2 kickjava
[1683]
By Anonymous on 2005/12/01 20:58:34 Rate
Thank you. I'm a newbie too & this helped me avoid this pitfall.
public E getFirst()
- See Also:
- NoSuchElementException
- Geek's Notes:
- Description Add your codes or notes Search More Java Examples
[946]Retrieve first item from LinkedList
By parisjackson { at } yahoo { dot } com on 2004/09/27 18:07:06 Rate
LinkedList list = new LinkedList ( ) ;
list.add ( "Shave" ) ;
list.add ( "And" ) ;
list.add ( "A" ) ;
list.add ( "Haircut" ) ;
list.add ( "Two" ) ;
list.add ( "Bits" ) ;
//Now list looks like
//- > "Shave"- > "And"- > "A"- > "Haircut"- > "Two"- > "Bits"
String s1 = ( String ) list.getFirst ( ) ; //s1 = "Shave"
public E getLast()
- See Also:
- NoSuchElementException
- Geek's Notes:
- Description Add your codes or notes Search More Java Examples
[947]Retrieve last item from LinkedList
By parisjackson { at } yahoo { dot } com on 2004/09/27 18:07:42 Rate
LinkedList list = new LinkedList ( ) ;
list.add ( "Shave" ) ;
list.add ( "And" ) ;
list.add ( "A" ) ;
list.add ( "Haircut" ) ;
list.add ( "Two" ) ;
list.add ( "Bits" ) ;
//Now list looks like
//- > "Shave"- > "And"- > "A"- > "Haircut"- > "Two"- > "Bits"
String s1 = ( String ) list.getLast ( ) ; //s1 = "Bits"
public int indexOf(Object o)
- See Also:
- E, AbstractList, List
- Geek's Notes:
- Description Add your codes or notes Search More Java Examples
[948]Check the position of an item in a LinkedList
By parisjackson { at } yahoo { dot } com on 2004/09/27 19:15:09 Rate
LinkedList list = new LinkedList ( ) ;
int position;
list.add ( "Shave" ) ;
list.add ( "And" ) ;
list.add ( "A" ) ;
list.add ( "Haircut" ) ;
list.add ( "Two" ) ;
list.add ( "Bits" ) ;
//Now list looks like
//- > "Shave"- > "And"- > "A"- > "Haircut"- > "Two"- > "Bits"
position = list.indexOf ( "Shave" ) ; //position = 0
position = list.indexOf ( "Two" ) ; //position = 4
public int lastIndexOf(Object o)
- See Also:
- E, AbstractList, List
- Geek's Notes:
- Description Add your codes or notes Search More Java Examples
[949]Check the last position of an item in a LinkedList
By parisjackson { at } yahoo { dot } com on 2004/09/27 19:19:18 Rate
LinkedList list = new LinkedList ( ) ;
list.add ( "Shave" ) ;
list.add ( "And" ) ;
list.add ( "A" ) ;
list.add ( "Haircut" ) ;
list.add ( "Two" ) ;
list.add ( "Bits" ) ;
//Now list looks like
//- > "Shave"- > "And"- > "A"- > "Haircut"- > "Two"- > "Bits"
position = list.LastIndexOf ( "Shave" ) ; //position = 0
list.add ( "Shave" ) ;
//Now list looks like
//- > "Shave"- > "And"- > "A"- > "Haircut"- > "Two"- > "Bits"- > "Shave"
position = list.LastIndexOf ( "Shave" ) ; //position = 6
public LinkedList()
- Geek's Notes:
- Description Add your codes or notes Search More Java Examples
public LinkedList(Collection<? extends E> c)
- See Also:
- NullPointerException
- Geek's Notes:
- Description Add your codes or notes Search More Java Examples
[950]Convert a stack to a LinkedList
By parisjackson { at } yahoo { dot } com on 2004/09/27 19:20:18 Rate
Stack stack = new Stack ( ) ;
stack.add ( "It" ) ;
stack.add ( "was" ) ;
stack.add ( "the" ) ;
stack.add ( "best" ) ;
stack.add ( "of" ) ;
stack.add ( "times" ) ;
//Now we have a collection implemented with a Stack that looks like
//- > "It"- > "was"- > "the"- > "best"- > "of"- > "times"
LinkedList list = new LinkedList ( stack ) ;
//Now we have a LinkedList named list that also looks like
//- > "It"- > "was"- > "the"- > "best"- > "of"- > "times"
public ListIterator<E> listIterator(int index)
- See Also:
- IndexOutOfBoundsException, List
- Geek's Notes:
- Description Add your codes or notes Search More Java Examples
public boolean offer(E o)
- See Also:
- Queue
- Geek's Notes:
- Description Add your codes or notes Search More Java Examples
public E peek()
- See Also:
- Queue
- Geek's Notes:
- Description Add your codes or notes Search More Java Examples
public E poll()
- See Also:
- Queue
- Geek's Notes:
- Description Add your codes or notes Search More Java Examples
public E remove()
- See Also:
- NoSuchElementException, Queue
- Geek's Notes:
- Description Add your codes or notes Search More Java Examples
public E remove(int index)
- See Also:
- IndexOutOfBoundsException, AbstractSequentialList, List
- Geek's Notes:
- Description Add your codes or notes Search More Java Examples
[835]Number of items in a LinkedList
By parisjackson { at } yahoo { dot } com on 2004/09/27 15:20:10 Rate
LinkedList l = new LinkedList ( ) ;
l.add ( new Integer ( 1 ) ) ;
int lSize = l.size ( ) ; //lSize = 1
l.remove ( 0 ) ;
lSize = = l.size ( ) ; //lSize = 0
public boolean remove(Object o)
- See Also:
- E, AbstractCollection, List, Collection
- Geek's Notes:
- Description Add your codes or notes Search More Java Examples
[951]Remove item from LinkedList
By parisjackson { at } yahoo { dot } com on 2004/09/27 20:13:47 Rate
LinkedList list = new LinkedList ( ) ;
Integer i1 = new Integer ( 1 ) ;
Integer i2 = new Integer ( 2 ) ;
list.add ( i1 ) ;
list.add ( i2 ) ;
//now we have a list that looks like
// - > 1- > 2
int lSize = list.size ( ) ; //lSize = 2
Object o = list.remove ( i1 ) ;
//now we have a list that looks like
// - > 2
lSize = list.size ( ) ; //lSize = 1
Integer i3 = ( Integer ) list.remove ( i2 ) ;
lSize = list.size ( ) ; //lSize = 0
public E removeFirst()
- See Also:
- NoSuchElementException
- Geek's Notes:
- Description Add your codes or notes Search More Java Examples
[952]Safely remove all the elements in a list
By parisjackson { at } yahoo { dot } com on 2004/09/27 20:16:30 Rate
LinkedList list = new LinkedList ( ) ;
Integer i1 = new Integer ( 1 ) ;
Integer i2 = new Integer ( 2 ) ;
list.add ( i1 ) ;
list.add ( i2 ) ;
//now we have a list that looks like
// - > 1- > 2
int lSize = list.size ( ) ; //lSize = 2
Object o = list.removeFirst ( ) ;
//now we have a list that looks like
// - > 2
lSize = list.size ( ) ; //lSize = 1
Integer i3 = ( Integer ) list.removeFirst ( ) ;
lSize = list.size ( ) ; //lSize = 0
[1515]_
By ficr { at } hotmail { dot } com on 2005/08/12 09:13:26 Rate
//Safely remove all the elements in a list
while ( list.size ( ) != 0 )
{
System.out.print ( list.getFirst ( ) + " "
try { list.removeFirst ( ) ; }
catch ( NoSuchElementException e ) { throw new RuntimeException ( e ) ; }
}
public E removeLast()
- See Also:
- NoSuchElementException
- Geek's Notes:
- Description Add your codes or notes Search More Java Examples
[953]Remove item from bottom of LinkedList
By parisjackson { at } yahoo { dot } com on 2004/09/27 20:17:32 Rate
LinkedList list = new LinkedList ( ) ;
Integer i1 = new Integer ( 1 ) ;
Integer i2 = new Integer ( 2 ) ;
Integer i3 = new Integer ( 3 ) ;
list.add ( i1 ) ;
list.add ( i2 ) ;
list.add ( i3 ) ;
//now we have a list that looks like
// - > 1- > 2- > 3
int lSize = list.size ( ) ; //lSize = 3
Object o = list.removeLast ( ) ;
//now we have a list that looks like
// - > 1- > 2
lSize = list.size ( ) ; //lSize = 2
Integer i4 = ( Integer ) list.removeLast ( ) ;
lSize = list.size ( ) ; //lSize = 1
//now we have a list that looks like
// - > 1
public E set(int index,
E element)
- See Also:
- IndexOutOfBoundsException, AbstractSequentialList, List
- Geek's Notes:
- Description Add your codes or notes Search More Java Examples
public Object set(int index,
Object element)
- See Also:
- IndexOutOfBoundsException, AbstractSequentialList, List
- Geek's Notes:
- Description Add your codes or notes Search More Java Examples
[954]Replace items in a LinkedList
By parisjackson { at } yahoo { dot } com on 2004/09/27 20:18:58 Rate
LinkedList list = new LinkedList ( ) ;
Integer i1 = new Integer ( 1 ) ;
Integer i2 = new Integer ( 2 ) ;
Integer i3 = new Integer ( 3 ) ;
list.add ( i1 ) ;
list.add ( i2 ) ;
list.add ( i3 ) ;
//now we have a list that looks like
// - > 1- > 2- > 3
int lSize = list.size ( ) ; //lSize = 3
Integer i4 = new Integer ( 4 ) ;
list.set ( 0, i4 ) ;
//now we have a list that looks like
// - > 4- > 2- > 3
Integer i5 = new Integer ( 5 ) ;
Integer i6 = list.set ( 1, i5 ) ;
int i = i6.intValue ( ) ; //i = 2
//now we have a list that looks like
// - > 4- > 5- > 3
public int size()
- See Also:
- E, AbstractCollection, List, Collection
- Geek's Notes:
- Description Add your codes or notes Search More Java Examples
[955]Add items to a LinkedList
By parisjackson { at } yahoo { dot } com on 2004/09/27 20:19:28 Rate
LinkedList list = new LinkedList ( ) ;
int lSize;
Integer i1 = new Integer ( 1 ) ;
Integer i2 = new Integer ( 2 ) ;
Integer i3 = new Integer ( 3 ) ;
list.add ( i1 ) ;
lSize = list.size ( ) ; //lSize = 1
list.add ( i2 ) ;
lSize = list.size ( ) ; //lSize = 2
list.add ( i3 ) ;
lSize = list.size ( ) ; //lSize = 3
//now we have a list that looks like
// - > 1- > 2- > 3
public Object[] toArray()
- See Also:
Arrays.asList(Object[])
, E, AbstractCollection, List, Collection
- Geek's Notes:
- Description Add your codes or notes Search More Java Examples
[1023]Convert list to array
By shiva { dot } s mailtomeshiva { at } yahoo { dot } com on 2004/10/07 05:57:45 Rate
LinkedList list = new LinkedList ( ) ;
list.add ( "shiva" ) ;
list.add ( "java" ) ;
list.add ( "world" ) ;
String [ ] str = list.toArray ( new String [ list.size ( ) ] ) ;
public Object[] toArray(Object[] a)
- See Also:
- ArrayStoreException, AbstractCollection, List
- Geek's Notes:
- Description Add your codes or notes Search More Java Examples
public <T> T[] toArray(T[] a)
- See Also:
- NullPointerException, ArrayStoreException, E, AbstractCollection, List, Collection
- Geek's Notes:
- Description Add your codes or notes Search More Java Examples