Adding Elements: add a single element : LinkedList « Collections « Java Tutorial

Java Tutorial
1. Language
2. Data Type
3. Operators
4. Statement Control
5. Class Definition
6. Development
7. Reflection
8. Regular Expressions
9. Collections
10. Thread
11. File
12. Generics
13. I18N
14. Swing
15. Swing Event
16. 2D Graphics
17. SWT
18. SWT 2D Graphics
19. Network
20. Database
21. Hibernate
22. JPA
23. JSP
24. JSTL
25. Servlet
26. Web Services SOA
27. EJB3
28. Spring
29. PDF
30. Email
31. J2ME
32. J2EE Application
33. XML
34. Design Pattern
35. Log
36. Security
37. Apache Common
38. Ant
39. JUnit
Java
Java Source Code / Java Documentation
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Tutorial » Collections » LinkedList 
9. 12. 5. Adding Elements: add a single element

Adding the element to the end of the list unless an index is specified.

public boolean add(Object element)
public boolean add(int index, Object element)

To Treat the linked list as a stack or queue:

public boolean addFirst(Object element)
public boolean addLast(Object element)

The addLast() method is equivalent to call add(), with no index.

import java.util.LinkedList;

public class MainClass {
  public static void main(String[] a) {

    LinkedList list = new LinkedList();
    list.add("A");
    list.add("B");
    list.add("C");
    list.add("D");
    list.addFirst("X");
    list.addLast("Z");

    System.out.println(list);
  }
}
[X, A, B, C, D, Z]
9. 12. LinkedList
9. 12. 1. LinkedList Class
9. 12. 2. Get first and last elements from LinkedList
9. 12. 3. Get elements from LinkedList
9. 12. 4. Get SubList from LinkedList
9. 12. 5. Adding Elements: add a single element
9. 12. 6. Add elements at beginning and end of LinkedList
9. 12. 7. LinkedList: add, addFirst, addLast, remove
9. 12. 8. Retrieving the Ends from a LinkedList
9. 12. 9. Removing Elements
9. 12. 10. Making a stack from a LinkedList
9. 12. 11. Making a queue from a LinkedList
9. 12. 12. Convert Collection to ArrayList
9. 12. 13. Remove all elements or clear LinkedList
9. 12. 14. Remove first and last elements of LinkedList
9. 12. 15. Remove range of elements from LinkedList
9. 12. 16. Remove specified element from LinkedList
9. 12. 17. Convert LinkedList to Array with zero length array
9. 12. 18. Convert LinkedList to Array with full length array
9. 12. 19. Convert a LinkedList to ArrayList
9. 12. 20. Implementing a Stack
9. 12. 21. Implementing a Queue with LinkedList
9. 12. 22. Wrap queue to synchronize the methods
9. 12. 23. Search elements of LinkedList
9. 12. 24. Replace an Element of LinkedList
9. 12. 25. Add object to LinkedList
9. 12. 26. Check if a particular element exists in LinkedList
9. 12. 27. Create an object array from elements of LinkedList
9. 12. 28. Using a LinkedList in multi-thread
9. 12. 29. A basic priority linked list: maintaining an individual LinkedList for each priority level.
9. 12. 30. Helper method for creating list
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.