Skip to content

Instantly share code, notes, and snippets.

@amirul12
Created October 14, 2016 15:13
java String Array Example
package com.edupointbd.amirul;
public class StringArrayExample {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
// declare a string array with initial size
String[] progrmmingBook = new String[4];
// add elements to the array
progrmmingBook[0] = "Programming in C ";
progrmmingBook[1] = "Java ";
progrmmingBook[2] = "C++";
progrmmingBook[3] = "Python";
String[] schoolbag2 = { "Bangla", "English", "Science", "Arts" };
// 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 : progrmmingBook) {
System.out.println(str);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment