Home >Java >javaTutorial >How to Correctly Initialize Arrays in Java?
Troubleshooting Array Initialization in Java
When initializing arrays in Java, it's essential to adhere to proper syntax and indexing conventions to avoid errors.
The Problem:
An attempt to initialize an array as follows results in an error:
data[10] = {10,20,30,40,50,60,71,80,90,91};
The Solution:
The problematic line is incorrect syntax for array initialization. To rectify this:
Use an Array Initializer:
int[] data = {10,20,30,40,50,60,71,80,90,91};
Or, Assign a New Array to a Declare Variable:
int[] data; data = new int[] {10,20,30,40,50,60,71,80,90,91};
Note the following important points:
The above is the detailed content of How to Correctly Initialize Arrays in Java?. For more information, please follow other related articles on the PHP Chinese website!