Java Tutorial
Operators
Flow Control
String
Number and Date
Built-in Classes
Array
Class and Object
Inheritance and Polymorphism
Exception Handling
Collections, Generics and Enumerations
Reflection
Input/Output Stream
Annotation
1D arrays, also known as one-dimensional arrays, are linear data structures that can hold a fixed number of elements of the same data type. In Java, arrays are objects that can be created and manipulated dynamically.
In this tutorial, we'll cover the following topics related to 1D arrays in Java:
Declaration and Initialization
Looping through the Array
Common Array Operations
Array Length
Declaration and Initialization:
There are several ways to declare and initialize a 1D array in Java:
A. Declare an array and then initialize it:
int[] numbers; // Declare an int array numbers = new int[5]; // Initialize the array with a length of 5
B. Declare and initialize an array in one line:
int[] numbers = new int[5]; // Declare and initialize an int array with a length of 5
C. Declare and initialize an array with specific elements:
int[] numbers = {1, 2, 3, 4, 5}; // Declare and initialize an int array with specific elements
There are several ways to loop through a 1D array in Java:
A. Using a for loop:
for (int i = 0; i < numbers.length; i++) { System.out.println(numbers[i]); }
B. Using an enhanced for loop (also known as a for-each loop):
for (int number : numbers) { System.out.println(number); }
A. Adding elements to the array:
numbers[0] = 1; // Add element at index 0 numbers[1] = 2; // Add element at index 1
B. Updating elements in the array:
numbers[0] = 5; // Update element at index 0
C. Accessing elements from the array:
int firstNumber = numbers[0]; // Get the element at index 0
To find the length of an array, use the length
attribute:
int arrayLength = numbers.length; // Get the length of the array
Note: Remember that the index of an array starts from 0 and goes up to the length of the array minus 1.
This tutorial should give you a basic understanding of 1D arrays in Java. With this knowledge, you can create, manipulate, and work with arrays effectively in your Java programs.
Java 1D Array Definition:
// Define a 1D array of integers int[] numbers;
Java 1D Array Declaration:
// Declare a 1D array of strings String[] names;
Initializing 1D Arrays in Java:
// Initialize an array of doubles double[] prices = new double[5];
Assigning Values to a 1D Array in Java:
// Assign values to an array prices[0] = 10.99; prices[1] = 5.0;
Java Array Instantiation:
// Instantiate an array of integers int[] ages = new int[3];
Java Array Length and Size:
length
property to get the size (number of elements) of an array.// Get the length of an array int size = ages.length;
Java Array Index and Access:
// Access array elements int firstAge = ages[0];
Java Array Literals for 1D Arrays:
// Initialize an array using literals int[] fibonacci = {0, 1, 1, 2, 3, 5, 8};
Java Dynamic Array Creation:
// Dynamic array creation using ArrayList ArrayList<Integer> dynamicArray = new ArrayList<>();
Java Array Initialization with Values:
// Initialize an array with values char[] vowels = {'a', 'e', 'i', 'o', 'u'};
Java Array Default Values:
// Default values in an integer array int[] defaultArray = new int[3]; // {0, 0, 0}
Java Array and Loop Initialization:
// Initialize an array using a loop int[] squares = new int[5]; for (int i = 0; i < 5; i++) { squares[i] = i * i; }
Copying 1D Arrays in Java:
// Copy an array int[] copyArray = Arrays.copyOf(originalArray, originalArray.length);
Java Array Sorting for 1D Arrays:
// Sort an array Arrays.sort(numbers);
Java Array Manipulation Techniques:
// Reverse an array Collections.reverse(Arrays.asList(array)); // Shuffle an array Collections.shuffle(Arrays.asList(array)); // Search for an element in an array int index = Arrays.binarySearch(sortedArray, key); // Filter elements in an array int[] filteredArray = Arrays.stream(numbers).filter(n -> n > 5).toArray();
Multidimensional Arrays in Java:
// 2D array int[][] matrix = {{1, 2, 3}, {4, 5, 6}};
Java Array Memory Allocation:
// Memory allocation for an array int[] memoryArray = new int[10];
Java Array vs ArrayList for 1D Data:
// Using ArrayList for dynamic resizing ArrayList<String> dynamicList = new ArrayList<>(); dynamicList.add("Item 1");