Java Array Examples
Also see: http://www.java.sun.com/docs/books/tutorial/java/data/arrays.html
See notes at the bottom of the page, below the program listing...
public class JavaSamples
{
public static void main(String args[])
{
char charArray[] = new char[10];
String string1 = new String("Hello World");
char charArray2[] = new char[80];
int i;
charArray[0] = 'A'; charArray[1] = 'B';
charArray[2] = 'C'; charArray[3] = 'D';
/* The above could also be initialized as:
charArray[] = {'A','B','C','D'};
OR
charArray[] = ("ABCD").toCharArray();
*/
System.out.println("Printing the character array:");
for(i = 0; i < charArray.length; i++) {
System.out.println(charArray[i] + " " + (int)charArray[i] +
" " + Integer.toBinaryString((int)charArray[i]) +
" " + Integer.toHexString((int)charArray[i]));
}
System.out.println();
System.out.println("Printing the string:");
for(i = 0; i < string1.length(); i++)
{
System.out.println(string1.charAt(i) + " " + (int)string1.charAt(i) +
" " + Integer.toBinaryString((int)string1.charAt(i)) +
" " + Integer.toHexString((int)string1.charAt(i)));
}
System.out.println();
System.out.println("Printing the string converted to a character array:");
charArray2 = string1.toCharArray();
for(i = 0; i < charArray2.length; i++) {
System.out.println(charArray2[i] + " " + (int)charArray2[i] +
" " + Integer.toBinaryString((int)charArray2[i]) +
" " + Integer.toHexString((int)charArray2[i]));
}
System.out.println();
}
}
- First, a character array is created:
char charArray[] = new char[10]
Note that a character array is different from a String class.
Also, character arrays and Strings in Java do not have to end in the null
character
- An instance of the String is created:
String string1 = new String("Hello World");
- An alternative syntax is:
String string1 = "Hello World";
This version implies the new String() creation.
Strings are special, and allow for this simpler syntax
- If you need to add more characters into a String or change the String,
use StringBuffer
(See the Java API site for more info on Strings and StringBuffers)
- In this example, characters are added individually into the char array
An alternative syntax is: charArray = ("String value").toCharArray() OR
charArray = someString.toCharArray();
- Another syntax to initialize arrays is:
charArray = {'H','e','l','l','o',' ','W','o','r','l','d'};
- To add characters in a StringBuffer, use the append() method:
strBuffer.append(someChar)
- In an array in Java, the .length field specifies the length of the array:
charArray.length
- In a String in Java, the length() method specifies the length of the String:
string1.length()
- To print the Unicode integer representation of a character use a typecast:
(int) charArray[i]
- Java uses Unicode instead of ASCII to represent character values
- To print the binary and hexadecimal representations of the characters,
use the toBinaryString() and toHexString() methods from the Integer class:
Integer.toBinaryString((int)someChar) and
Integer.toHexString((int)someChar)
These two methods return a String