Java String Array Sample Code

Arrays in Java programming are very useful when used the right way. The Java String Array is a container that holds a list of Strings. The code snippets below provide examples what can be done with String Arrays. There is also a working code sample below that includes all of the snippets.


String Array Declare

Declare a String Array with a fixed length of 4.

String[] arrayString1 = new String[4];



String Array Initialize

Once the String Array has been declared, you can initialize it with values. Remember to keep in mind the index always starts at 0.

arrayString1[0] = "One";
arrayString1[1] = "Two";
arrayString1[2] = "Three";
arrayString1[3] = "Four";



String Array Declare & Initialize

Declare a String Array and initialize it with values at the same time.

String[] arrayString2 = { "One", "Two", "Three", "Four" };



String Array Iterate For Loop

A for loop can be used to iterate the contents of the String Array.

for(int x=0; x < arrayString2.length; x++)
{
	System.out.println("Array Elements: "+arrayString2[x]);
}



String Array Iterate Advanced Loop

The Java advanced loop is another way to iterate the contents of the String Array.

for(String element : arrayString2)
{
	System.out.println("Array Elements: "+element);
}



String Array Copy To String Array

The clone() method will allow you to make a copy or clone of the String Array and assign it to another String Array.

String[] arrayString3 = arrayString2.clone();



String Array Copy To String

The Arrays.toString() method will convert the String Array to a String.

String myString = Arrays.toString(arrayString3);



String Array Copy To List

The Arrays.asList() method will copy the String Array to a List object.

 List<String> myList = Arrays.asList(arrayString3);



String Array Copy To Set

The HashSet Class and Arrays.asList() method will copy the String Array to a Set object.

Set mySet = new HashSet(Arrays.asList(arrayString3));



Java String Array Sample Code:

package com.jcd.java.tutorials;

import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

public class JavaArrays {
	
	
	public static void main(String[] args) 
	{
		//Declare String Array with a Fixed Size
		String[] arrayString1 = new String[4];
		
		//Manually assign values to String Array
		//Remember indexes start at 0
		arrayString1[0] = "One";
		arrayString1[1] = "Two";
		arrayString1[2] = "Three";
		arrayString1[3] = "Four";
		
		//Declare String Array and Initialize Values
		String[] arrayString2 = { "One", "Two", "Three", "Four" };
		
		//Loop through array with for loop and print array elements
		for(int x=0; x < arrayString2.length; x++)
		{
			System.out.println("Array Elements: "+arrayString2[x]);
		}
		
		System.out.println("\n---------------------------------------------\n");
		
		//Loop through array with the advanced for loop method
		for(String element : arrayString2)
		{
			System.out.println("Array Elements: "+element);
		}
		
		//Print out the length arrayString2
		System.out.println("\nLength of Array: "+arrayString2.length);
		
		//Clone or copy the string array into a new string array
		String[] arrayString3 = arrayString2.clone();
		
		//Print out the length arrayString3
		System.out.println("\nLength of Cloned Array: "+arrayString3.length);
		
		//Convert the string array to a string using Array.toString method
		String myString = Arrays.toString(arrayString3);
		
		//Print out new String myString
		System.out.println("\nmyString: "+myString);
		
		//Convert the string array to a string using Array.toString method
	    List<String> myList = Arrays.asList(arrayString3);
		
	    System.out.println("\nmyList: "+myList.toString());
	    
	    //Set<String> mySet = new HashSet<String>(Arrays.asList(myList));
	    Set mySet = new HashSet(Arrays.asList(arrayString3));
	    
	    System.out.println("\nmySet: "+mySet.toString());
	    
	}

}


Java String Array Sample Code Output:

Array Elements: One
Array Elements: Two
Array Elements: Three
Array Elements: Four

---------------------------------------------

Array Elements: One
Array Elements: Two
Array Elements: Three
Array Elements: Four

Length of Array: 4

Length of Cloned Array: 4

myString: [One, Two, Three, Four]

myList: [One, Two, Three, Four]

mySet: [One, Four, Two, Three]



Java References:

Add a Comment

Your email address will not be published. Required fields are marked *