KaiquanMah's picture
String[] names = new String[16]; arr.length; list.size(); str.length();
ec3e65d verified
raw
history blame
3.36 kB
Arrays are static data structures: their SIZE is defined AT INITIALISATION and cannot be changed afterwards.
It is therefore not possible to add or remove primitives from an array.
In general, a list is a more useful structure, but some Java methods return an array- so it's a good idea to know how to use them.
// datatype of elements
// empty square brackets []
// variable name
// 'new' operator - define the array object
// datatype of elements
// num_elements
public class Example {
public static void main(String[] args){
// Array in which 10 integers
// can be saved
int[] numarray = new int[10];
// Array for strings
String[] names = new String[20];
// Array for floating-point numbers
double[] results = new double[5];
}
}
At initialisation, fill array with default values
- integer 0
- boolean false
- null
VS
// initialise Array with values
// Array with five integers
int[] numarray = {1, 3, 2, 5, -1};
// Array with strings
String[] names = {"Jack", "Pete", "Jane", "Lisa"};
// Array with floating-point numbers
double[] results = {1.0, 2.5, 0.75};
======================================================
import java.util.Arrays;
public class Example {
public static void main(String[] args){
int[] numbers = new int[5];
numbers[0] = 10; // variableName[idx]
numbers[1] = 40;
numbers[4] = -155;
// When printing an array the method
// Arrays.toString is useful.
System.out.println(Arrays.toString(numbers)); // [10, 40, 0, 0, -155]
double[] heights = {175.5, 150.75, 201.05};
System.out.println(heights[0]);
heights[1] = heights[1] + 10; // variableName[idx]
System.out.println(heights[1]); // variableName[idx]
}
}
Program outputs:
[10, 40, 0, 0, -155]
175.5
160.75
======================================================
length
The length of the array (i.e. the number of sub-arrays) is determined by the length attribute.
The extended for clause can also be used to iterate through an array in the same way as iterating through a list.
import java.util.Arrays;
public class Example {
public static void main(String[] args){
int[] numbers = {1,2,3,4,5,6,7};
System.out.println("Array length: " + numbers.length);
for (int element : numbers) {
System.out.println(element);
}
for (int i=0; i<numbers.length; i++) {
numbers[i]++;
}
System.out.println(Arrays.toString(numbers));
}
}
Program outputs:
Array length: 7
1
2
3
4
5
6
7
[2, 3, 4, 5, 6, 7, 8]
======================================================
Note that unlike a string, ARRAY LENGTH is an attribute
ARRAY length is not a method - it is not followed by parentheses.
The example below further illustrates the differences between length detection for a string, an array and a "list":
int[] array = {1,2,3,4,5,6,7};
ArrayList<Integer> list = new ArrayList<>();
list.add(1);
list.add(2);
String str = "Hey";
System.out.println("Array length: " + array.length);
System.out.println("List length: " + list.size());
System.out.println("String length: " + str.length());
Program outputs:
Array length: 7
List length: 2
String length: 3