Home » Java Basics » arrays » Java String Array Example

About Konstantina Dimtsa

Konstantina Dimtsa
Konstantina has graduated from the Department of Informatics and Telecommunications in National and Kapodistrian University of Athens (NKUA) and she is currently pursuing M.Sc studies in Advanced Information Systems at the same department. She is also working as a research associate for NKUA in the field of telecommunications. Her main interests lie in software engineering, web applications, databases and telecommunications.

Java String Array Example

In this example we will show how to declare and populate a string array as well as how to iterate through all its elements.

1. Example of string array

Create a java class named JavaStringArrayExample.java with the following code:

Do you want to know how to develop your skillset to become a Java Rockstar?

Subscribe to our newsletter to start Rocking right now!

To get you started we give you our best selling eBooks for FREE!

1. JPA Mini Book

2. JVM Troubleshooting Guide

3. JUnit Tutorial for Unit Testing

4. Java Annotations Tutorial

5. Java Interview Questions

6. Spring Interview Questions

7. Android UI Design

and many more ....

 

JavaStringArrayExample.java

package com.javacodegeeks.javabasics.stringarray;

public class JavaStringArrayExample {

	public static void main(String args[]) {

		// declare a string array with initial size
		String[] schoolbag = new String[4];

		// add elements to the array
		schoolbag[0] = "Books";
		schoolbag[1] = "Pens";
		schoolbag[2] = "Pencils";
		schoolbag[3] = "Notebooks";

		// this will cause ArrayIndexOutOfBoundsException
		// schoolbag[4] = "Notebooks";

		// declare a string array with no initial size
		// String[] schoolbag;

		// declare string array and initialize with values in one step
		String[] schoolbag2 = { "Books", "Pens", "Pencils", "Notebooks" };

		// print the third element of the string array
		System.out.println("The third element is: " + schoolbag2[2]);

		// iterate all the elements of the array
		int size = schoolbag2.length;
		System.out.println("The size of array is: " + size);
		for (int i = 0; i < size; i++) {
			System.out.println("Index[" + i + "] = " + schoolbag2[i]);
		}

		// iteration provided by Java 5 or later
		for (String str : schoolbag2) {
			System.out.println(str);
		}

	}

}

Let’s give a short explanation of the above code. First, we declare a string array with an initial size of 4 elements and then add 4 elements. If you try to add one more element, you will have java.lang.ArrayIndexOutOfBoundsException, as the capacity of the array is fixed and cannot be resized once created. If you want a dynamically sized array where you can add and remove the objects and the size is adjusted accordingly, you should use an ArrayList. You can have a look at How to use ArrayList example.
Next, we define another string array named schoolbag2 as it is more common to declare and populate a string array in one step. Finally, we show two ways of iterating through all the elements of the array. The first one is a more “traditional way”, by using the standard for loop mechanism and the size of the array. The second way uses the enhanced for loop example, which was introduced in Java 5.

If we run the above code, we will have the following results:

  • Output:
The third element is: Pencils
The size of array is: 4
Index[0] = Books
Index[1] = Pens
Index[2] = Pencils
Index[3] = Notebooks
Books
Pens
Pencils
Notebooks

2. Download the source code

This was an example of string array in Java. You can download the source code from here: StringArrayExample.zip

Do you want to know how to develop your skillset to become a Java Rockstar?

Subscribe to our newsletter to start Rocking right now!

To get you started we give you our best selling eBooks for FREE!

 

1. JPA Mini Book

2. JVM Troubleshooting Guide

3. JUnit Tutorial for Unit Testing

4. Java Annotations Tutorial

5. Java Interview Questions

6. Spring Interview Questions

7. Android UI Design

 

and many more ....

 

 

Want to take your Java skills to the next level?

Grab our programming books for FREE!

Here are some of the eBooks you will get:

  • Spring Interview QnA
  • Multithreading & Concurrency QnA
  • JPA Minibook
  • JVM Troubleshooting Guide
  • Advanced Java
  • Java Interview QnA
  • Java Design Patterns