Java array FAQ: Can you show some Java array examples, specifically some Java String array examples, as well as the Java 5 for loop syntax?
Sure. In this tutorial, we demonstrate how to declare, populate, and iterate through a Java String array, including many String array source code examples, including the Java 5 for loop syntax.
You can declare a Java String array and give it an initial size like this:
public class JavaStringArrayTests
{
private String[] toppings = new String[20];
// more to the class here ...
}
In that example, we create a Java String array named toppings, where the toppings array has been given an initial size of 20 elements.
Later on, in a Java method in your class, you can populate the elements in the String array like this:
void populateStringArray()
{
toppings[0] = "Cheese";
toppings[1] = "Pepperoni";
toppings[2] = "Black Olives";
// ...
}
As you can see, a Java array (in this case a String array) begins with an element numbered zero (they are zero-based), just like the C programming language.
You can also declare a Java String array without giving it an initial size, like this:
public class JavaStringArrayTests
{
private String[] toppings;
// more to the class here ...
}
Then later on in your code you can give your Java array a size, and populate it as desired, like this:
void populateStringArray()
{
toppings = new String[20];
toppings[0] = "Cheese";
toppings[1] = "Pepperoni";
toppings[2] = "Black Olives";
// ...
}
This approach is very similar to the previous approach, but as you can see, we don't give the String 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.)
You don't have to declare a Java 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:
toppings array is defined and populated in one step.toppings array has only three elements in it.Before Java 5, the way to loop through a String 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()
{
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();
}
}
With the advent of Java 5, we can make our for loops a little cleaner and easier to read, so looping through a Java String 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()
{
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 a Java String array is more concise, and easier to read.
As I wrap up my collection of "Java String array" tutorials, here are a few links to my current String array in Java tutorials:
thanks, i too struggle with
thanks, i too struggle with the java array syntax. these examples are very helpful.
Very thorough examples -
Very thorough examples - thanks!
Thanks for these examples!
Thanks for these examples! Showing different ways to create string arrays is very helpful, as is the Java 5 for loop syntax example.
Thanks!
Thanks!
this is great
thank you !!!
Post new comment