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 Java 5 for loop syntax?

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

Back to top

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 this 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 you can see, Java arrays begins with an element numbered zero -- they are zero-based -- just like the C programming language.

Back to top

2) Declare a Java String array with no initial size

If you know you need a String array, but don't initially know how large the array needs to be, you can also 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 give your array a size, and 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. (If you'd like to know all the differences, just leave a comment below, and I'll be glad to provide more details.)

Back to top

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 our 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.
Back to top

4) Iterating through a String array - Before Java 5

Before Java 5, the way to loop through an array involved getting the number of elements in the array, and then 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();
    }
}
Back to top

5) Iterating through a String array - After Java 5

With the advent of Java 5, we can make our 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 new 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.

Back to top

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 array tutorials I just finished writing:

Back to top
Permalink

Thanks for these examples! Showing different ways to create string arrays is very helpful, as is the Java 5 for loop syntax example.

Add new comment

The content of this field is kept private and will not be shown publicly.

Anonymous format

  • Allowed HTML tags: <em> <strong> <cite> <code> <ul type> <ol start type> <li> <pre>
  • Lines and paragraphs break automatically.
By submitting this form, you accept the Mollom privacy policy.