Java – Initialize String Array

To initialize Cord Array in Java, define a string array and assign a set of elements to the array, or define a string array with specific size and assign values to the array using index.

Initialize String Array with Set of Strings

Declare a variable of type Cord[] and assign ready of strings to it using assignment operator.

In the post-obit program, we will initialize a string assortment fruits with 4 string elements.

Java Program

public grade Example { 	public static void chief(String[] args){ 		String fruits[] = {"apple", "assistant", "mango", "orange"}; 		 		for(Cord fruit: fruits) { 			Organisation.out.println(fruit); 		} 	} }

Output

apple banana mango orange

Initialize String Array – 2d Method

As already mentioned in the introduction, nosotros tin can initialize a cord array by defining a string assortment with specific size, then assigning values to its elements using alphabetize.

In the following program, we will ascertain a string array fruits of size four and assign values to the elements using index.

Java Program

public class Instance { 	public static void main(String[] args){ 		String fruits[] = new Cord[four]; 		fruits[0] = "apple"; 		fruits[ane] = "banana"; 		fruits[2] = "mango"; 		fruits[3] = "orange"; 		 		for(String fruit: fruits) { 			System.out.println(fruit); 		} 	} }

Output

apple banana mango orange

Conclusion

In this Java Tutorial, we learned different ways to initialize String Assortment in Java, with the help of instance programs.