Declaring a String array

As explained in Arrays section, you can declare a String array like this:

package com.ibytecode.strings.arrays;
public class StringArray {
	private String[] fruits; //instance variable initialized to null
}

Then later on, you can write and call a method which creates an array, and initializes the elements, like this:

void initializeArray() {
		fruits = new String[5];
		fruits[0] = "Apple";
		fruits[1] = "Orange";
		fruits[2] = "Banana";
		fruits[3] = "Grapes";
		fruits[4] = "Mango";
}

Declaring and creating a String array with an initial size

You can declare and create a String array like this:

package com.ibytecode.strings.arrays;
public class StringArray {

	private String[] fruits = new String[5];
	
	public static void main(String[] args) {
		StringArray obj = new StringArray();
		obj.initializeArray();
	}

	void initializeArray() {
		fruits[0] = "Apple";
		fruits[1] = "Orange";
		fruits[2] = "Banana";
		fruits[3] = "Grapes";
		fruits[4] = "Mango";
	}
}

In the above example, we created a String array named fruits with an initial size 5.
Later, in a method initializeArray() we initialized the array elements.

This picture demonstrates the result of line 4.

When a String array is declared and created, its element gets default value ‘null’.

Declaring, creating and initializing a String array

You can declare, create and initialize a String array in one step.

String fruit = "Pineapple";
String[] fruits = {"Apple", "Orange", "Banana", fruit, "Strawberry"};

How this works?

  • Creates a String object “Pineapple” in the memory and fruit referencing to it.
  • Declares a String array reference variable fruits.
  • Creates a String array object in the memory with a length of five.
    • Size of an array is determined by the number of comma-separated elements between the curly braces.
  • Creates four String objects for “Apple”, “Orange”, “Banana” and “Strawberry” and assigns its references to the array indices (0, 1, 2 and 4). Assigns already created String object for “Pineapple” to the index 3.
  • Assigns the String array object to array reference variable fruits.

Creating an anonymous String array

You can declare, create and initialize a String array object in one line.

String fruit = "Pineapple";
String[] fruits = new String[]{"Apple", "Orange", "Banana", fruit, "Strawberry"};

This is similar to the previous method but this method is useful when you want to create a just-in-time String array to use as an argument to a method that takes a String array parameter.
Example

package com.ibytecode.strings.parampassing;
public class StringArray {
	public static void main(String[] args) {
		method(new String[]{"Apple", "Orange"});
	}

	static void method(String[] fruits)
	{
		//Use String[] array here.
	}
}

When you use this method size should not be specified.
String[] fruits = new String[2] {“Apple”, “Orange”}; //ERROR

Iterating through a String array

Using length variable

You can use any loop (for, while, do-while) to iterate through a String array starting from first element till array’s length which you will get by using arrayName.length

package com.ibytecode.strings.arrays;
public class IteratingStringArray {
	public static void main(String[] args) {
		String fruit = "Pineapple";
		String[] fruits = {"Apple", "Orange", "Banana",fruit,"Strawberry"};
		//for loop
		for(int i = 0; i < fruits.length; i++)
			System.out.println("Fruit at index " + i + ": " + fruits[i]);

		//while loop
		int i = 0;
		while(i < fruits.length)
		{
			System.out.println("Fruit at index " + i + ": " + fruits[i]);
			i++;
		}
	}
}

Fruit at index 0: Apple
Fruit at index 1: Orange
Fruit at index 2: Banana
Fruit at index 3: Pineapple
Fruit at index 4: Strawberry

Enhanced for loop

This is a specialized for loop introduced in Java SE 6 to loop through an array or collection.
It has two parts for(declaration : expression)

declaration newly declared variable of type String.
expression this should be an array or collection variable name or a method call that returns a String array.

Syntax:
for(type variable : arrayReferenceName)
{
//Use variable
}
where, type of the variable should be the type of the array.

package com.ibytecode.strings.arrays;
public class IteratingStringArray {
	public static void main(String[] args) {
		String fruit = "Pineapple";
		String[] fruits = {"Apple", "Orange", "Banana",fruit,"Strawberry"};
		for(String s: fruits)
			System.out.print(s + “ ”);
	}
}

Apple Orange Banana Pineapple Strawberry

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.