How to Update Items in a List With Java

How to Update Items in a List With Java thumbnail
How to Update Items in a List With Java

One of the powerful features of the Java programming language is its implementation of many powerful and common data structures (besides the ordinary array), as part of its built-in package of classes. One of these data structures is the collection of classes that derive from the List interface. Unlike ordinary arrays, lists can be efficiently and easily resized to accommodate data sets that regularly change size. However, it is not without its disadvantages: one is that, once a list is built, it can be difficult to modify the items; simply retrieving the items from the list and changing them is ineffective. The retrieval of items creates a new instance of the item, so the original in the list does not reflect changes.

Instructions

    • 1

      Open your favorite text editor. Any will do, from Windows Notepad to the Netbeans Java development environment. Paste the following skeleton code to get started:

      import java.util.List;

      import java.util.ArrayList;

      public class SimpleListTutorial {

      public static void main(String[] args) {

      }

      }

      All the code for this tutorial will go within the "main" brackets.

    • 2

      Create a list of strings by using the following code:

      List<String> l = new ArrayList<String>();

      l.add("First item");

      l.add("Second item");

      l.add("Fourth item");

    • 3

      Add an item to the middle of the list with the following code:

      l.add(2, "Third item");

      This adds the string "Third item" to the third place in the list. Remember, list indexes, like all counting in Java, starts at 0, rather than 1.

    • 4

      Modify an item in a list into something else. Use the "set" method:

      l.set(2, "A new third item");

      This will completely replace the item in the third slot with the new item given. It is the same as the following code:

      l.remove(2);

      l.add(2, "A new third item.");

    • 5

      Print the current list to ensure it worked:

      for(String s : l) {

      System.out.println(s);

      }

Related Searches:

References

  • Photo Credit Comstock Images/Comstock/Getty Images

Comments

You May Also Like

  • How to Update a String Array in Java Dynamic

    The Java programming language features many classes, which are digital plans or blueprints for creating virtual objects. One type of class is...

  • How to Update Your Version of Java

    Created by Sun Microsystems, the Java programming language allows developers to compile their programs into class files. These class files are capable...

  • How to Turn Off Automatic Java Updates in Windows 7

    The Oracle Corporation's Java Runtime Environment software gives your computer the ability to run any program created using the Java programming language....

  • How to Remove Java 6 Update 10

    Certain programs and websites require that you have a version of Java on your computer. Java periodically releases product updates that add...

  • How to Update an XML File in Java

    The Word Wide Web Consortium (W3C), which publishes the XML standard, has provided a number of classes in the Java standard library...

  • How to Sort a Linked List in Java

    A linked list is one of the primary types of data structures in the programming world. It's an arrangement of nodes which...

  • Sun Java Swing Tutorials

    Sun Java Swing Tutorials. Java is a popular software platform developed by Sun Microsystems that known for its platform-independent, "write once, run...

  • How to Delete Java Version 5 Update 6

    Java is a requisite for running many PC games, utilities, as well as personal and business applications. The Java computer language is...

  • How to Delete Java Updates

    Java is a software platform developed by Sun Microsystems that allows you to run a variety of web and desktop applications. Some...

Related Ads

Featured