An array can be defined as a collection of variables of the same type defined by a common name, e.g. an array called Names which stores the names of your class mates:
Names [name1, name2, name3, … nameX]
Arrays in Java are different from arrays in other programming languages because they are implemented as objects.
Declaration: type array-name[ ] = new type[size];
e.g. int sample[] = new int[10];
The following code creates an array of ten integers, fills it up with numbers using a loop and then prints the content of each location (index) of the array:
class ArrayDemo { public static void main(String args[]) { int sample[] = new int[10]; int i; for(i = 0; i < 10; i = i+1) { sample[i] = i; for(i = 0; i < 10; i = i+1) System.out.println("This is sample[" + i + "]: " + sample[i]); } } } // O/P: This is sample[0]: 0 This is sample[1]: 1 This is sample[2]: 2 This is sample[3]: 3 This is sample[4]: 4 This is sample[5]: 5 This is sample[6]: 6 This is sample[7]: 7 This is sample[8]: 8 This is sample[9]: 9
The following program contains two loops used to identify the smallest and the largest value stored in the array:
class MinMax { public static void main(String args[]) { int nums[] = new int[10]; int min, max; nums[0] = 99; nums[1] = -10; nums[2] = 100123; nums[3] = 18; nums[4] = -978; nums[5] = 5623; nums[6] = 463; nums[7] = -9; nums[8] = 287; nums[9] = 49; min = max = nums[0]; for(int i=1; i < 10; i++) { if(nums[i] < min) min = nums[i]; if(nums[i] > max) max = nums[i]; } System.out.println("min and max: " + min + " " + max); } } // O/P : min and max: -978 100123
The Bubble sort is one type, the simplest, of sorting algorithms. It can be used to sort data stored in arrays but it is not ideal when the size of the array to sort is large.
class Bubble { public static void main(String args[]) { int nums[] = { 99, -10, 100123, 18, -978, 5623, 463, -9, 287, 49 }; int a, b, t; int size; size = 10; // number of elements to sort // display original array System.out.print("Original array is:"); for(int i=0; i < size; i++) System.out.print(" " + nums[i]); System.out.println(); // This is the Bubble sort. for(a=1; a < size; a++) for(b=size-1; b >= a; b–-) { if(nums[b-1] > nums[b]) { // if out of order // exchange elements t = nums[b-1]; nums[b-1] = nums[b]; nums[b] = t; } } // display sorted array System.out.print("Sorted array is:"); for(int i=0; i < size; i++) System.out.print(" " + nums[i]); System.out.println(); } } // O/P : Original array is: 99 -10 100123 18 -978 5623 463 -9 287 49 Sorted array is: -978 -10 -9 18 49 99 287 463 5623 100123
A two dimensional array is like a list of one-dimensional arrays. Declaration is as follows :
int table[][] = new int[10][20];
This would create a table made up of 10 rows (index: 0 to 9) and 20 columns (index: 0 to 19). The following code creates a table, 3 rows by 4 columns, and fills it up woth numbers from 1 to 12. Note that at index [0][0] = 1, [0][1] = 2, [0][2] = 3, [0][3] = 4, [1][0] = 5, etc.
class TwoD { public static void main(String args[]) { int t, i; int table[][] = new int[3][4]; for(t=0; t < 3; ++t) { for(i=0; i < 4; ++i) { table[t][i] = (t*4)+i+1; System.out.print(table[t][i] + " "); } System.out.println(); } } }
Different syntax used to declare arrays :
Consider the following : type[ ] var-name;
The square brackets follow the type specifier, not the name of the array variable.
For example, the following two declarations are equivalent:
int counter[] = new int[3]; int[] counter = new int[3];
The following declarations are also equivalent :
char table[][] = new char[3][4]; char[][] table = new char[3][4];
This alternative declaration form offers convenience when declaring several arrays at the same time.
int[] nums, nums2, nums3; // create three arrays
This creates three array variables of type int. It is the same as writing :
int nums[], nums2[], nums3[]; // also, create three arrays
The alternative declaration form is also useful when specifying an array as a return type for a method:
int[] someMeth( ) { ...
This declares that someMeth( ) returns an array of type int.
Consider a particular array, nums1, and another array, nums2 and at one point in the code we assign one array reference to the other, i.e. nums2 = nums1. Then every action on nums2 will be as if it were on nums1 (nums2 reference is lost).
class AssignARef { public static void main(String args[]) { int i; int nums1[] = new int[10]; int nums2[] = new int[10]; for(i=0; i < 10; i++) nums1[i] = i; for(i=0; i < 10; i++) nums2[i] = -i; System.out.print("Here is nums1: "); for(i=0; i < 10; i++) System.out.print(nums1[i] + " "); System.out.println(); System.out.print("Here is nums2: "); for(i=0; i < 10; i++) System.out.print(nums2[i] + " "); System.out.println(); nums2 = nums1; // nums2 is now nums1 System.out.print("Here is nums2 after assignment: "); for(i=0; i < 10; i++) System.out.print(nums2[i] + " "); System.out.println(); //now operate on nums1 array through nums2 nums2[3] = 99; System.out.print("Here is nums1 after change through nums2: "); for(i=0; i < 10; i++) System.out.print(nums1[i] + " "); System.out.println(); } } O/P : Here is nums1: 0 1 2 3 4 5 6 7 8 9 Here is nums2: 0 -1 -2 -3 -4 -5 -6 -7 -8 -9 Here is nums2 after assignment: 0 1 2 3 4 5 6 7 8 9 Here is nums1 after change through nums2: 0 1 2 99 4 5 6 7 8 9
All Rights Reserved. © 2024 BookOfNetwork