Mastering Java One-Dimensional Arrays from Scratch
This tutorial walks through the fundamentals of Java one-dimensional arrays, covering declaration syntax, memory allocation with new, default values, direct initialization, element access, out‑of‑bounds handling, and element modification with concrete code examples and expected outputs.
In Java, a one‑dimensional array is a linear collection of elements of the same type, each accessed by a single index. It forms the basic building block for more complex multi‑dimensional arrays.
Using a one‑dimensional array involves four steps: declare a variable with the array type, create the array object (allocate memory), store data in the array, and then access or modify the stored data.
Declaration : Java supports two equivalent syntaxes:
int[] array; // preferred form int array[];The first form is usually chosen because the brackets are placed next to the type, clearly indicating the element type.
Creation with new : After declaring the variable, allocate the array with a specified size, e.g.:
int[] array = new int[10]; // creates an int array of length 10 String[] teacher_names = new String[20];When created, each element receives a default value: numeric types → 0, boolean → false, char → '\0', object references → null.
Direct initialization : The array size can be inferred from a brace‑enclosed list of values:
int[] array = {1,2,3,4,5,6,7,8}; String[] teacher_names = {"王老师","李老师","张老师"};Accessing elements : Use the array name followed by the index in square brackets. The following example demonstrates declaration, creation, and safe access, as well as the runtime exception thrown when an index is out of bounds:
public class ElementAccess1 {
public static void main(String[] args) {
int nums[] = new int[2];
String names[] = new String[5];
System.out.println(nums[0]); // prints 0
System.out.println(nums[1]); // prints 0
// System.out.println(nums[2]); // would throw ArrayIndexOutOfBoundsException
System.out.println(names[0]); // prints null
System.out.println(names[1]); // prints null
System.out.println(names[2]); // prints null
System.out.println(names[3]); // prints null
System.out.println(names[4]); // prints null
// System.out.println(names[5]); // would throw ArrayIndexOutOfBoundsException
}
}Running the program yields the output:
0
0
null
null
null
null
null
nullModifying elements : Although an array’s length is fixed after creation, its individual elements can be reassigned. The following example creates arrays for Chinese and English scores, student names, and a sum array, then computes and prints each student’s total score:
public class ElementModification {
public static void main(String[] args) {
int chineseScore[] = {60,70,80};
int englishScore[] = new int[3];
int sum[] = new int[3];
String[] student_names = {"小明","小红","小花"};
englishScore[0] = 88;
englishScore[1] = 77;
englishScore[2] = 66;
sum[0] = chineseScore[0] + englishScore[0];
sum[1] = chineseScore[1] + englishScore[1];
sum[2] = chineseScore[2] + englishScore[2];
System.out.println(student_names[0] + "的总分数为:" + sum[0]);
System.out.println(student_names[1] + "的总分数为:" + sum[1]);
System.out.println(student_names[2] + "的总分数为:" + sum[2]);
}
}The program prints:
小明的总分数为:148
小红的总分数为:147
小花的总分数为:146In real‑world scenarios the amount of data far exceeds these small examples, so developers typically process arrays with loops rather than hard‑coded element accesses.
Lisa Notes
Lisa's notes: musings on daily life, work, study, personal growth, and casual reflections.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
