Java String array examples (with Java 5 for loop syntax)

Java String array FAQ: Can you share some Java array examples, specifically some String array examples, as well as the new for loop syntax that was introduced back in Java 5?

Sure. In this tutorial, I’ll show how to declare, populate, and iterate through Java string arrays, including the newer for-loop syntax. Because creating a String array is just like creating and using any other Java object array, these examples also work as more generic object array examples.

1) Declaring a Java String array with an initial size

If you know up front how large your array needs to be, you can (a) declare a String array and (b) give it an initial size, like this:

public class JavaStringArrayTests
{
    private String[] toppings = new String[20];

    // more ...
}

In that example, I declare a String array named toppings, and then give it an initial size of 20 elements.

Later on, in a Java method in your class, you can populate the elements in the array like this:

private void populateStringArray()
{
    toppings[0] = "Cheese";
    toppings[1] = "Pepperoni";
    toppings[2] = "Black Olives";

    // ...
}

As this example shows, Java arrays begins with an element numbered zero; they are zero-based, just like the C programming language.

2) Declare a Java String array with no initial size

If you need a String array but don’t initially know how large the array needs to be, you can declare an array variable without giving it an initial size, like this:

public class JavaStringArrayTests
{
    private String[] toppings;

    // more ...
}

Then somewhere later in your code you can (a) give the array a size and then (b) populate it as desired, like this:

private void populateStringArray()
{
    // you'll probably determine this size based on some algorithm
    toppings = new String[20];

    toppings[0] = "Cheese";
    toppings[1] = "Pepperoni";
    toppings[2] = "Black Olives";

    // ...
}

This array programming approach is very similar to the previous approach, but as you can see, I don't give the array a size until the populateStringArray method is called.

3) Declaring and populating a Java String array

You don’t have to declare a String array in two steps, you can do everything in one step, like this:

public class JavaStringArrayTests
{
    private String[] toppings = {"Cheese", "Pepperoni", "Black Olives"};
}

This example is similar to the previous example, with the following differences:

  1. The toppings array is defined and populated in one step.
  2. This toppings array has only three elements in it.

4) Iterating through a String array: Before Java 5

Before Java 5, the way to loop through an array involved (a) getting the number of elements in the array, and then (b) looping through the array with a for loop. Here’s a complete source code example that demonstrates the syntax prior to Java 5:

public class JavaStringArrayTests1
{
    private String[] toppings = {"Cheese", "Pepperoni", "Black Olives"};

    // our constructor; print out the String array here
    public JavaStringArrayTests1()
    {
        // old `for` loop
        int size = toppings.length;
        for (int i=0; i<size; i++)
        {
            System.out.println(toppings[i]);
        }
    }

    // main kicks everything off.
    // create a new instance of our class here.
    public static void main(String[] args)
    {
        new JavaStringArrayTests1();
    }
}

5) Iterating through a String array: After Java 5

With the advent of Java 5, you can make your for loops a little cleaner and easier to read, so looping through an array is even easier. Here’s a complete source code example that demonstrates the Java 5 syntax:

public class JavaStringArrayTests2
{

    private String[] toppings = {"Cheese", "Pepperoni", "Black Olives"};

    // our constructor; print out the String array here
    public JavaStringArrayTests2()
    {
        // new `for` loop
        for (String s: toppings)
        {
          System.out.println(s);
        }
    }

    // main kicks everything off.
    // create a new instance of our class here.
    public static void main(String[] args)
    {
        new JavaStringArrayTests2();
    }
}

I think you’ll agree that the Java 5 syntax for looping through an array is more concise, and easier to read.

Related

I hope these Java String array examples have been helpful. As you can see, there are several different ways to work with arrays in Java.

Before I go, here are a few links to other Java array tutorials I have written: