A character array in Java is an array that stores multiple characters (char) in contiguous memory locations. It is useful when you want to manipulate individual characters of a string-like data structure or perform operations such as sorting, searching, or reversing characters.
Example:
class GFG {
public static void main(String[] args) {
char[] charArray1 = new char[5];
char[] charArray2 = {'a', 'b', 'c', 'd', 'e'};
System.out.println("Array 1 length: " + charArray1.length);
System.out.println("Array 2 length: " + charArray2.length);
}
}
Output
Array 1 length: 5 Array 2 length: 5
Explanation:
- charArray1 = new char[5] creates an empty character array of length 5.
- charArray2 = {'a','b','c','d','e'} creates a character array and assigns values directly.
- charArray.length is used to get and print the length of the arrays.
Declaring a Character Array
char[] charArray;
charArray = new char[10];
or
char[] charArray = new char[10];
or
char[] charArray = {'a', 'b', 'c', 'd', 'e'};
Operations on Character Array in Java
The following are the main operations you can perform on a character array (char[]):
1. Accessing Elements
Use the index to access or modify elements:
charArray[0] = 'f';
System.out.println(charArray[0]);
Example: This program demonstrates how to access and modify elements in a character array in Java.
class GFG {
public static void main(String[] args) {
char[] charArray = {'x', 'y', 'z'};
System.out.println("First element: " + charArray[0]);
System.out.println("Second element: " + charArray[1]);
charArray[0] = 'a';
System.out.println("Modified first element: " + charArray[0]);
}
}
Output
First element: x Second element: y Modified first element: a
Explanation:
- charArray[0] and charArray[1] are used to access elements at index 0 and 1.
- charArray[0] = 'a' modifies the first element of the array.
2. Iterating Over a Character Array
Example 1: This program demonstrates how to iterate over a character array using a standard for loop in Java.
class GFG {
public static void main(String[] args) {
char[] charArray = {'H', 'e', 'l', 'l', 'o'};
for (int i = 0; i < charArray.length; i++) {
System.out.print(charArray[i] + " ");
}
System.out.println();
}
}
Output
H e l l o
Explanation:
- for (int i = 0; i < charArray.length; i++) loops through each index of the array.
- charArray[i] accesses the element at the current index i.
Example 2: This program demonstrates how to iterate over a character array using the enhanced for-each loop in Java.
class GFG {
public static void main(String[] args) {
char[] charArray = {'H', 'e', 'l', 'l', 'o'};
for (char c : charArray) {
System.out.print(c + " ");
}
}
}
Output
H e l l o
Explanation:
- for (char c : charArray) iterates directly over each element of the array.
- c represents the current character in the iteration.
3. Comparing Character Arrays
Example: This program demonstrates how to compare two character arrays for equality using Arrays.equals() in Java.
import java.util.Arrays;
class GFG {
public static void main(String[] args) {
char[] arr1 = {'a','b','c','d','e'};
char[] arr2 = {'a','b','c','d','e'};
char[] arr3 = {'a','b','c','x','y'};
if (Arrays.equals(arr1, arr2))
System.out.println("arr1 and arr2 are equal");
else
System.out.println("arr1 and arr2 are not equal");
if (Arrays.equals(arr1, arr3))
System.out.println("arr1 and arr3 are equal");
else
System.out.println("arr1 and arr3 are not equal");
}
}
Output
arr1 and arr2 are equal arr1 and arr3 are not equal
Explanation: Arrays.equals() compares the contents of the arrays, not references.
4. Converting Between Strings and Character Arrays
Example 1: This program demonstrates how to convert a String into a character array in Java.
import java.util.Arrays;
class GFG {
public static void main(String[] args) {
String str = "Hello World";
char[] charArray = str.toCharArray();
System.out.println("Character Array: " + Arrays.toString(charArray));
}
}
Output
Character Array: [H, e, l, l, o, , W, o, r, l, d]
Explanation:
- str.toCharArray() converts the string str into a char array.
- Arrays.toString(charArray) is used to print the array in a readable format.
Example 2: This program demonstrates how to convert a character array into a String in Java.
import java.util.Arrays;
class GFG {
public static void main(String[] args) {
char[] charArray = {'H','e','l','l','o'};
String str = new String(charArray);
System.out.println("String: " + str);
}
}
Output
String: Hello
Explanation: new String(charArray) creates a String from the character array charArray.
5. Copying Character Arrays
Example 1: This program demonstrates how to create a full copy of a character array using Arrays.copyOf() in Java.
import java.util.Arrays;
class GFG {
public static void main(String[] args) {
char[] original = {'a','b','c','d','e'};
char[] copy = Arrays.copyOf(original, original.length);
System.out.println("Original: " + Arrays.toString(original));
System.out.println("Full Copy: " + Arrays.toString(copy));
}
}
Explanation:
- Arrays.copyOf(original, original.length) creates a new array copy containing all elements of original.
- Arrays.toString() is used to print arrays in a readable format.
- Changes to copy will not affect the original array since it’s a separate copy.