C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML
For: The for-loop syntax can be used on an int array in two ways—with a ":" over elements, or in the standard indexing syntax.
ForAlso: We initialize an array in separate statements. This is the "alternative" array, but this style is standard and clear.
Java program that creates int arrays
public class Program {
public static void main(String[] args) {
// Create int array with four elements.
int[] values = { 10, 20, 30, 40 };
// ... Loop over the array's elements.
for (int value : values) {
System.out.println(value);
}
// Create int array with three elements in separate statements.
int[] alternative = new int[3];
alternative[0] = 100;
alternative[1] = 200;
alternative[2] = -100;
// ... Use for-loop to access all elements.
for (int i = 0; i < alternative.length; i++) {
System.out.println(alternative[i]);
}
}
}
Output
10
20
30
40
100
200
-100
Java program that returns an int array
public class Program {
static int[] getEmployees() {
// Create an int array and return it.
int[] array = new int[6];
array[0] = 9;
array[1] = 11;
array[2] = 15;
array[3] = 19;
array[4] = 29;
array[5] = 55;
return array;
}
public static void main(String[] args) {
// Loop over an array returned by a method.
for (int e : getEmployees()) {
System.out.println(e);
}
}
}
Output
9
11
15
19
29
55
AddResidentAt: This method increments the apartmentIds array at a specified index. In a real program, it might check for a valid index.
RemoveResidentAt: This is the same style of method as addResidentAt but it subtracts from the element at the array index.
GetOccupancyAt: This method returns the value of the element at an index. In main, we use it and the other two methods.
Java program that uses int array in class
class Building {
int[] apartmentIds = new int[10];
public void addResidentAt(int id) {
// Add to the array at this index.
apartmentIds[id]++;
}
public void removeResidentAt(int id) {
// Subtract from element value.
apartmentIds[id]--;
}
public int getOccupancyAt(int id) {
// Return element value.
return apartmentIds[id];
}
}
public class Program {
public static void main(String[] args) {
// Create a Building.
// ... Some residents move in and one leaves.
Building b = new Building();
b.addResidentAt(5);
b.addResidentAt(3);
b.addResidentAt(9);
b.removeResidentAt(5);
// Display occupancy of apartments.
for (int i = 0; i < 10; i++) {
System.out
.println(Integer.toString(i) + ": " + b.getOccupancyAt(i));
}
}
}
Output
0: 0
1: 0
2: 0
3: 1
4: 0
5: 0
6: 0
7: 0
8: 0
9: 1
Note: Even in a for-loop, we must first check for a null array. A NullPointerException will otherwise occur.
Java program that causes an exception
public class Program {
public static void main(String[] args) {
int[] array = null;
// We must first check for null before looping over an array.
// ... This causes a runtime error.
for (int value : array) {
System.out.println(value);
}
}
}
Output
Exception in thread "main" java.lang.NullPointerException
at program.Program.main(Program.java:9)
However: If the array's size is uncertain, we must use an if-check to ensure an exception does not occur.
IfJava program that uses invalid element index
public class Program {
public static void main(String[] args) {
// This array has just 5 elements.
// ... So the only valid indexes are 0, 1, 2, 3 and 4.
int[] array = { 40, 50, -60, -70, 80 };
// This causes an exception.
array[10] = 1000;
}
}
Output
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 10
at program.Program.main(Program.java:7)
So: When a cloned array is modified, the original does not reflect those changes.
Java program that uses clone
public class Program {
public static void main(String[] args) {
int[] items = { 10, 20, 40 };
// Clone the int array.
// ... All the elements are copied into a new array.
int[] copy = items.clone();
// When the copy is modified, the original "items" is not affected.
copy[0] = -100;
System.out.println(items[0]);
System.out.println(copy[0]);
}
}
Output
10
-100