How to Add to a String Array in Java

How to Add to a String Array in Java thumbnail
Java String processing includes text gathered from user input.

Handling String arrays is a common task in Java programming. Adding an element to a Java collection is something developers do regularly. Java arrays can be created, accessed and edited in a number of ways. As with all programming tasks, to create an efficient application it is best to equip yourself with some knowledge regarding how structures such as arrays and other collection classes are actually implemented. Arrays are not generally intended to be extended, so adding an element to one requires a series of steps.

Instructions

    • 1

      Create a String array using the following syntax:

      String [] myStrings = new String [10];

    • 2

      Instantiate the array with String elements:

      for(int i=0; i<myStrings.length; i++) {

      myStrings[i]="x";

      }

      This writes a test string into each element position; if you have another String array you can use it instead.

    • 3

      Prepare the String you want to add to the array using an existing String or creating a new one and storing it in a variable as follows:

      String addString = "y";

      Replace "y" with your String, which can contain any alphanumeric characters or punctuation symbols.

    • 4

      Convert the array to a List collection. Arrays are designed to have a fixed length, specified on creation. For this reason it is best to temporarily convert your array to an ArrayList object before extending it. You can do this using the following syntax:

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

      Collections.addAll(stringList, myStrings);

      Your IDE (Integrated Development Environment) may prompt you with an error message regarding the ArrayList and Collections classes. Address this by including the following import statements at the top of your class declaration:

      import java.util.ArrayList;

      import java.util.Collections;

    • 5

      Add the new String to the ArrayList. Use the "add" method to add the new String to the end of the ArrayList object:

      stringList.add(addString);

      This method appends the new element to the end of the collection, extending the length by one and inserting the new String at this final position.

    • 6

      Convert the ArrayList back to an array. Replace the original array variable reference with the new ArrayList converted to a String array:

      myStrings = (String[])stringList.toArray(new String[0]);

      This code uses the "toArray" method of the ArrayList class to convert and cast the collection back to a String array object.

    • 7

      Test the new array:

      System.out.println("Array length: " + myStrings.length);

      System.out.println("Final element: " + myStrings[myStrings.length-1]);

    • 8

      Save the file, compile and run the program to test it, observing the messages written to the output console.

Tips & Warnings

  • If you plan to add and remove elements from a collection, it might be best to use a List object instead of an array.

  • When you add an element to an array, you replace the original object with a new one, causing the process to use up memory unnecessarily.

Related Searches:

References

Resources

  • Photo Credit Creatas Images/Creatas/Getty Images

Comments

You May Also Like

Related Ads

Featured