Arrays are a fundamental data structure in Java, used to store multiple values of the same type in a single variable. Understanding how to perform common operations on arrays is essential for efficient programming.
Creating and Initializing Arrays
Syntax
dataType[ ] arrayName = new dataType [ arraySize] ; ขับเคลื่อนโดย
dataType : The type of elements the array will hold (e.g., int, String).
arrayName : The name of the array.
arraySize : The number of elements the array can hold.
Example
int[ ] numbers = new int [ 5 ] ;
String[ ] names = { "Alice" , "Bob" , "Charlie" } ; ขับเคลื่อนโดย
Accessing Array Elements
Array elements are accessed using their index, starting from 0.
Example
int[ ] numbers = { 10 , 20 , 30 } ;
System. out. println ( numbers[ 0 ] ) ; ขับเคลื่อนโดย
Modifying Array Elements
You can modify elements by assigning a new value to a specific index.
Example
int[ ] numbers = { 10 , 20 , 30 } ;
numbers[ 1 ] = 50 ; ขับเคลื่อนโดย
Iterating Over Arrays
Using a for Loop
int[ ] numbers = { 10 , 20 , 30 } ;
for ( int i = 0 ; i < numbers. length; i++ ) {
System. out. println ( numbers[ i] ) ;
} ขับเคลื่อนโดย
Using a for-each Loop
int[ ] numbers = { 10 , 20 , 30 } ;
for ( int number : numbers) {
System. out. println ( number) ;
} ขับเคลื่อนโดย
Finding the Length of an Array
The length of an array is accessed using the length property.
Example
int[ ] numbers = { 10 , 20 , 30 } ;
System. out. println ( "Array length: " + numbers. length) ; ขับเคลื่อนโดย
Copying Arrays
Using System.arraycopy()
int[ ] source = { 1 , 2 , 3 } ;
int[ ] destination = new int [ 3 ] ;
System. arraycopy ( source, 0 , destination, 0 , source. length) ; ขับเคลื่อนโดย
Using Arrays.copyOf()
import java. util. Arrays;
int[ ] source = { 1 , 2 , 3 } ;
int[ ] destination = Arrays. copyOf ( source, source. length) ; ขับเคลื่อนโดย
Sorting Arrays
Using Arrays.sort()
import java. util. Arrays;
int[ ] numbers = { 3 , 1 , 2 } ;
Arrays. sort ( numbers) ; ขับเคลื่อนโดย
Searching Arrays
Using Arrays.binarySearch()
import java. util. Arrays;
int[ ] numbers = { 1 , 2 , 3 , 4 , 5 } ;
int index = Arrays. binarySearch ( numbers, 3 ) ; ขับเคลื่อนโดย
Note : The array must be sorted before using binarySearch().
Tips and Best Practices
Bounds Checking : Always ensure you are accessing valid indices to prevent ArrayIndexOutOfBoundsException.
Immutable Arrays : Consider using Collections.unmodifiableList() if you need an immutable array-like structure.
Performance : Use System.arraycopy() for better performance when copying large arrays.
Use Enhanced For Loop : Prefer the for-each loop for readability when you do not need the index.
Library Functions : Utilize utility methods from the Arrays class for operations like sorting and searching to simplify code.
Arrays are a fundamental data structure in Java, used to store multiple values of the same type in a single variable. Understanding how to perform common operations on arrays is essential for efficient programming.
Creating and Initializing Arrays
Syntax
int,String).Example
Accessing Array Elements
Array elements are accessed using their index, starting from 0.
Example
Modifying Array Elements
You can modify elements by assigning a new value to a specific index.
Example
Iterating Over Arrays
Using a
forLoopUsing a
for-eachLoopFinding the Length of an Array
The length of an array is accessed using the
lengthproperty.Example
Copying Arrays
Using
System.arraycopy()Using
Arrays.copyOf()Sorting Arrays
Using
Arrays.sort()Searching Arrays
Using
Arrays.binarySearch()Note: The array must be sorted before using
binarySearch().Tips and Best Practices
ArrayIndexOutOfBoundsException.Collections.unmodifiableList()if you need an immutable array-like structure.System.arraycopy()for better performance when copying large arrays.for-eachloop for readability when you do not need the index.Arraysclass for operations like sorting and searching to simplify code.