Presentation is loading. Please wait.

Presentation is loading. Please wait.

Java Arrays. Array Object An array :a container object holds a fixed number of values of a single type. The length of an array is established when the.

Similar presentations


Presentation on theme: "Java Arrays. Array Object An array :a container object holds a fixed number of values of a single type. The length of an array is established when the."— Presentation transcript:

1 Java Arrays

2 Array Object An array :a container object holds a fixed number of values of a single type. The length of an array is established when the array is created. After creation, its length is fixed.

3 Array element and indexing Each item in an array: called an element each element is accessed by its numerical index. Numbering begins with 0. The 9th element, for example, would therefore be accessed at index 8.

4 ArrayDemo Class ArrayDemo { public static void main(String[] args) { // declares an array of integers int[] anArray; // allocates memory for 10 integers anArray = new int[10]; // initialize first element anArray[0] = 100; // initialize second element anArray[1] = 200; // and so forth anArray[9] = 1000; System.out.println("Element at index 0: " + anArray[0]); //Similarly elements at 1 –9 }

5 Declaring Variable to Refer to an Array Declaring a Variable to Refer to an Array : // declares an array of integers int[] anArray; array declaration has two components: the array's type and the array's name. An array's type is written as type[], where type is the data type of the contained elements ; ( the brackets are special symbols indicating that this variable holds an array ) The size of the array is not part of its type (which is why the brackets are empty). An array's name can be anything you want, provided that it follows the rules and conventions in the naming section. As with variables of other types, the declaration does not actually create an array; it simply tells the compiler that this variable will hold an array of specified type.naming

6 Other array types byte[] anArrayOfBytes; short[] anArrayOfShorts; long[] anArrayOfLongs; float[] anArrayOfFloats; double[] anArrayOfDoubles; boolean[] anArrayOfBooleans; char[] anArrayOfChars; String[] anArrayOfStrings; You can also place the brackets after the array's name: // this form is discouraged float anArrayOfFloats[];

7 Creating, Initializing, and Accessing an Array One way to create an array is with the new operator. The next statement in the ArrayDemo program allocates an array with enough memory for 10 integer elements and assigns the array to the anArray variable. // create an array of integers anArray = new int[10]; If this statement is missing, then compiler prints an error like the following, and compilation fails: ArrayDemo.java:4: Variable anArray may not have been initialized

8 Alternate method of initialization The length of the array is determined by the number of values provided between braces and separated by commas.

9 You can also declare an array of arrays (also known as a multidimensional array) by using two or more sets of brackets, such as String[][] names. Each element, therefore, must be accessed by a corresponding number of index values. MultiDimensional Arrays

10 Length property of array you can use the built-in length property to determine the size of any array. The following code prints the array's size to standard output : System,out,println(anArray.length); System.out.println(anArray.length);

11 Array Copy

12 copyOfRange the previous example can be modified to use the copyOfRange method of the java.util.Arrays class, as you can see in the ArrayCopyOfDemo example. ArrayCopyOfDemo The difference is that using the copyOfRange method does not require you to create the destination array before calling the method, the destination array is returned by the method:

13 Useful operations of java.util.Arrays class

14 Anonymous arrray smallPrimes = new int[] { 17, 19, 23,29,31,37};

15 String args[] array

16 Passing Java Array to method

17 Matrix Multiplication

18 Matrix Multiplication… http://www.programmingsimplified.com/java/source- code/java-program-multiply-two-matrices

19 Sorting using Java Arrays Java 2D Arrays

20 Bubble Sort public class BubbleSortExample { static void bubbleSort(int[] arr) { int n = arr.length; int temp = 0; for(int i=0; i < n; i++){ for(int j=1; j < (n-i); j++){ if(arr[j-1] > arr[j]){ //swap elements temp = arr[j-1]; arr[j-1] = arr[j]; arr[j] = temp; } } } }

21 Bubble Sort… public static void main(String[] args) { int arr[] ={3,60,35,2,45,320,5}; System.out.println("Array Before Bubble Sort"); for(int i=0; i < arr.length; i++){ System.out.print(arr[i] + " "); } System.out.println(); bubbleSort(arr);//sorting array elements using bubble sort System.out.println("Array After Bubble Sort"); for(int i=0; i < arr.length; i++){ System.out.print(arr[i] + " "); }

22 Sorting Algos https://www.youtube.com/watch?v=cqh8nQwuKNE Selection Sort https://www.youtube.com/watch?v=F13_wsHDIG4 Bubble Sort https://www.youtube.com/watch?v=iMT7gTPpaqw Merge Sort

23 2D Array Sorting

24 Arrays.sort() method // A sample Java program to sort an array of integers // using Arrays.sort(). It by default sorts in // ascending order import java.util.Arrays; public class SortExample { public static void main(String[] args) { // Our arr contains 8 elements int[] arr = {13, 7, 6, 45, 21, 9, 101, 102}; Arrays.sort(arr); System.out.printf("Modified arr[] : %s", Arrays.toString(arr)); } Modified arr[] : [6, 7, 9, 13, 21, 45, 101, 102] A Java program to sort an array of integers in ascending order. Output: Modified arr[] : [6, 7, 9, 13, 21, 45, 101, 102]

25 Arrays.sort() method // A sample Java program to sort an array of integers // using Arrays.sort(). It by default sorts in // ascending order import java.util.Arrays; public class SortExample { public static void main(String[] args) { // Our arr contains 8 elements int[] arr = {13, 7, 6, 45, 21, 9, 101, 102}; Arrays.sort(arr); System.out.printf("Modified arr[] : %s", Arrays.toString(arr)); } Modified arr[] : [6, 7, 9, 13, 21, 45, 101, 102] A Java program to sort an array of integers in ascending order. Output: Modified arr[] : [6, 7, 9, 13, 21, 45, 101, 102]

26 Sorting subarray import java.util.Arrays; public class SortExample { public static void main(String[] args) { // Our arr contains 8 elements int[] arr = {13, 7, 6, 45, 21, 9, 2, 100}; // Sort subarray from index 1 to 5, i.e., // only sort subarray {7, 6, 45, 21} and // keep other elements as it is. Arrays.sort(arr, 1, 5); System.out.printf("Modified arr[] : %s", Arrays.toString(arr)); } Modified arr[] : [13, 6, 7, 21, 45, 9, 2, 100] Output:

27 Descending Order Sort import java.util.Arrays; import java.util.Collections; public class SortExample { public static void main(String[] args) { // Note that we have Integer here instead of // int[] as Collections.reverseOrder doesn't // work for primitive types. Integer[] arr = {13, 7, 6, 45, 21, 9, 2, 100}; // Sorts arr[] in descending order Arrays.sort(arr, Collections.reverseOrder()); System.out.printf("Modified arr[] : %s", Arrays.toString(arr)); }


Download ppt "Java Arrays. Array Object An array :a container object holds a fixed number of values of a single type. The length of an array is established when the."

Similar presentations


Ads by Google