1 / 42

Arrays in Java | Introduction to Java Arrays | Java Programming | Edureka

**** Java Certification Training: https://www.edureka.co/java-j2ee-soa-training **** <br>This Edureka tutorial on u201cArrays in Javau201d will talk about one of the pillars of Java fundamentals i.e Arrays. It will also take you through the various types of arrays in Java and how they are used to achieve various functionalities. Through this tutorial, you will learn the following topics: <br>1. Arrays in Java <br>2. Types of Arrays <br>3. Working with Arrays <br>4. Sorting in Arrays <br>5. Searching in Arrays <br><br>Check out our Java Tutorial blog series: https://goo.gl/osrGrS <br>Check out our complete Youtube playlist here: https://goo.gl/gMFLx3 <br><br>Follow us to never miss an update in the future. <br><br>Instagram: https://www.instagram.com/edureka_learning/ <br>Facebook: https://www.facebook.com/edurekaIN/ <br>Twitter: https://twitter.com/edurekain <br>LinkedIn: https://www.linkedin.com/company/edureka

EdurekaIN
Download Presentation

Arrays in Java | Introduction to Java Arrays | Java Programming | Edureka

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Topics For Today’s Discussion Topics for Today’s Session Array in Java Types of Arrays Declaring Accessing Updating Creating Working with Array Sorting in Arrays Searching in Arrays JAVA CERTIFICATION TRAINING www.edureka.co/java-j2ee-soa-training

  2. Java Arrays

  3. Java Array Contains collections of homogenous elements It occupies a contiguous memory location All the elements are stored under one variable name Array is a static Data Structure JAVA CERTIFICATION TRAINING www.edureka.co/java-j2ee-soa-training

  4. Types of Arrays

  5. Types Of Arrays 02 01 03 Single Dimensional Two Dimensional Multi Dimensional Single Dimensional or 1-D array is a type of linear array in which elements are stored in a continuous row Two Dimensional or 2-D array is a type of matrix in which elements are stored in rows and columns Multi Dimensional array is a type of nested array JAVA CERTIFICATION TRAINING www.edureka.co/java-j2ee-soa-training

  6. Working With Arrays

  7. Working With Arrays – 1D Declaring and Initializing an One Dimensional Array arrayRefVar = new dataType[arraySize]; = myArray new int[5] datatype[] arrayRefVar = new dataType[arraySize]; = int[5] myArray new int[5] JAVA CERTIFICATION TRAINING www.edureka.co/java-j2ee-soa-training

  8. Working With Arrays – 1D Declaring and Initializing an One Dimensional Array myArray = myArray[0] 10 myArray[0] myArray[1] myArray[2] myArray[3] myArray[4] JAVA CERTIFICATION TRAINING www.edureka.co/java-j2ee-soa-training

  9. Working With Arrays – 1D Declaring and Initializing an One Dimensional Array myArray = myArray[1] 20 = myArray[2] 30 10 = myArray[3] 40 myArray[0] myArray[1] myArray[2] myArray[3] myArray[4] = myArray[4] 50 JAVA CERTIFICATION TRAINING www.edureka.co/java-j2ee-soa-training

  10. Working With Arrays – 1D Declaring and Initializing an One Dimensional Array myArray 10 30 20 40 50 myArray[0] myArray[1] myArray[2] myArray[3] myArray[4] JAVA CERTIFICATION TRAINING www.edureka.co/java-j2ee-soa-training

  11. Working With Arrays – 1D Declaring and Initializing an One Dimensional Array dataType[] arrayRefVar = new dataType{e1,e2,e3,…,eN}; 20 = 30 40 50 int[] myArray new int{10,20,30,40,50} 10 myArray[0] myArray[1] myArray[2] myArray[3] myArray[4] JAVA CERTIFICATION TRAINING www.edureka.co/java-j2ee-soa-training

  12. Working With Arrays – 1D Declaring and Initializing an One Dimensional Array dataType[] arrayRefVar = new dataType{e1,e2,e3,…,eN}; int[] myArray 10 30 20 40 50 myArray[0] myArray[1] myArray[2] myArray[3] myArray[4] JAVA CERTIFICATION TRAINING www.edureka.co/java-j2ee-soa-training

  13. Working With Arrays – 1D Accessing a specific array element arrayRefVar[index] 10 myArray[0] myArray[1] 20 myArray[1] 30 myArray[4] myArray[2] 40 myArray[3] 50 myArray[4] JAVA CERTIFICATION TRAINING www.edureka.co/java-j2ee-soa-training

  14. Working With Arrays – 1D Accessing a specific array element arrayRefVar[index] 10 myArray[0] myArray[1] myArray[4] 20 myArray[1] 30 myArray[2] Updating a specific array element 40 myArray[3] arrayRefVar[index] = newValue myArray[4] = 100 50 100 myArray[4] JAVA CERTIFICATION TRAINING www.edureka.co/java-j2ee-soa-training

  15. Working With Arrays – 1D Accessing a specific array element arrayRefVar[index] 10 myArray[0] myArray[1] myArray[4] 20 myArray[1] 30 myArray[2] Updating a specific array element 100 myArray[3] arrayRefVar[index] = newValue myArray[4] = 100 50 myArray[4] JAVA CERTIFICATION TRAINING www.edureka.co/java-j2ee-soa-training

  16. Working With Arrays – 2D Declaring and Initializing an Two Dimensional Array datatype[][] arrayRefVar = new dataType[row][col]; = int[][] myArray new int[2][2] myArray[0][0] myArray[0][1] = myArray[0][0] 100 myArray[1][0] myArray[1][1] JAVA CERTIFICATION TRAINING www.edureka.co/java-j2ee-soa-training

  17. Working With Arrays – 2D Declaring and Initializing an Two Dimensional Array datatype[][] arrayRefVar = new dataType[row][col]; = int[][] myArray new int[2][2] = myArray[0][0] myArray[0][1] myArray[0][1] 200 = 100 myArray[1][0] 300 = myArray[1][1] 400 myArray[1][0] myArray[1][1] JAVA CERTIFICATION TRAINING www.edureka.co/java-j2ee-soa-training

  18. Working With Arrays – 2D Declaring and Initializing an Two Dimensional Array datatype[][] arrayRefVar = new dataType[row][col]; = int[][] myArray new int[2][2] myArray[0][0] myArray[0][1] 100 200 300 400 myArray[1][0] myArray[1][1] JAVA CERTIFICATION TRAINING www.edureka.co/java-j2ee-soa-training

  19. Working With Arrays – 2D Accessing a specific array element arrayRefVar[row][col] myArray[0][0] myArray[0][1] myArray[0][1] 100 200 Updating a specific array element 300 400 myArray[1][0] myArray[1][1] arrayRefVar[row][col] = newValue myArray[0][1] = 564 564 JAVA CERTIFICATION TRAINING www.edureka.co/java-j2ee-soa-training

  20. Working With Arrays – 2D Accessing a specific array element arrayRefVar[row][col] myArray[0][0] myArray[0][1] myArray[0][1] 100 564 Updating a specific array element 300 400 myArray[1][0] myArray[1][1] arrayRefVar[row][col] = newValue myArray[0][1] = 564 JAVA CERTIFICATION TRAINING www.edureka.co/java-j2ee-soa-training

  21. Operations With Arrays Addition of Matrices 1 2 1 1 2 3 5 4 4 5 3 6 1 7 2 7 8 9 a[3][3] b[3][3] JAVA CERTIFICATION TRAINING www.edureka.co/java-j2ee-soa-training

  22. Operations With Arrays a[3][3] 1 4 2 5 3 6 9 int[][] c = new int[rows][columns]; 7 8 for (int i = 0; i < rows; i++) { 1 5 2 4 1 3 2 for (int j = 0; j < columns; j++) 1 7 { b[3][3] c[i][j] = a[i][j] + b[i][j]; } 2 9 4 9 4 9 11 } 8 15 c[3][3] JAVA CERTIFICATION TRAINING www.edureka.co/java-j2ee-soa-training

  23. Operations With Arrays 1 2 3 1 2 1 Subtraction 4 5 6 5 4 3 Multiplication Division 7 8 9 1 7 2 b[3][3] a[3][3] JAVA CERTIFICATION TRAINING www.edureka.co/java-j2ee-soa-training

  24. Sorting in Arrays

  25. Sorting in Array Java offers various sorting algorithms that helps in putting elements of a list in a certain order Types of Sorting Algorithms Bubble Sort Insertion Sort Merge Sort Selection Sort Quick Sort JAVA CERTIFICATION TRAINING www.edureka.co/java-j2ee-soa-training

  26. Bubble Sort Selection Sort Insertion Sort Quick Sort Merge Sort 4 1 10 -3 12 void bubbleSort(int arr[]) { int n = arr.length; for (int i = 0; i < n-1; i++) for (int j = 0; j < n-i-1; j++) if (arr[j] > arr[j+1]) { // swap temp and arr[i] int temp = arr[j]; arr[j] = arr[j+1]; arr[j+1] = temp; } } 4 1 1 1 1 4 4 4 10 -3 12 10 -3 12 10 -3 12 -3 10 12 1 1 1 4 -3 -3 -3 10 12 4 10 12 4 10 12 1 -3 -3 1 4 4 10 12 10 12 -3 1 4 10 12 JAVA CERTIFICATION TRAINING www.edureka.co/java-j2ee-soa-training

  27. Bubble Sort Selection Sort Insertion Sort Quick Sort Merge Sort void selectionSort(int arr[]) { int n = arr.length; 4 1 10 -3 12 // One by one move boundary of unsorted subarray for (int i = 0; i < n-1; i++) { // Find the minimum element in unsorted array int min_idx = i; for (int j = i+1; j < n; j++) if (arr[j] < arr[min_idx]) min_idx = j; 4 1 10 -3 12 -3 1 10 4 12 -3 1 10 4 12 // Swap the found minimum element with the first element int temp = arr[min_idx]; arr[min_idx] = arr[i]; arr[i] = temp; -3 1 4 10 12 } } JAVA CERTIFICATION TRAINING www.edureka.co/java-j2ee-soa-training

  28. Bubble Sort Selection Sort Insertion Sort Quick Sort Merge Sort 4 1 10 -3 12 public static int[] insertionSort(int[] input){ 4 1 10 -3 12 int temp; for (int i = 1; i < input.length; i++) { for(int j = i ; j > 0 ; j--){ if(input[j] < input[j-1]){ // Swap with the smallest value temp = input[j]; input[j] = input[j-1]; input[j-1] = temp; } } } return input; 1 4 10 -3 12 1 4 10 -3 12 -3 1 4 10 12 } -3 1 4 10 12 JAVA CERTIFICATION TRAINING www.edureka.co/java-j2ee-soa-training

  29. Bubble Sort Selection Sort Insertion Sort Quick Sort Merge Sort private void quickSort(int low, int high) { int i = low; int j = high; 2 6 7 4 1 -2 8 4 3 // pivot is middle index int pivot = input[low + (high - low) / 2]; 2 6 7 4 1 -2 8 4 3 // Divide into two arrays while (i <= j) { while (input[i] < pivot) { i++; } while (input[j] > pivot) { j--; } if (i <= j) { swap(i, j); // move index to next position on both sides i++; j--; } } 2 6 7 3 1 -2 8 4 4 2 3 1 6 7 -2 8 4 4 2 3 1 -2 4 6 8 7 4 2 3 1 -2 4 4 8 7 6 JAVA CERTIFICATION TRAINING www.edureka.co/java-j2ee-soa-training

  30. swap(i, j); // move index to next position on both sides i++; j--; Quick Sort Bubble Sort Selection Sort Insertion Sort Merge Sort } } 2 6 7 4 1 -2 8 4 3 // calls quickSort() method recursively if (low < j) { quickSort(low, j); } 2 6 7 4 1 -2 8 4 3 2 6 7 3 1 -2 8 4 4 if (i < high) { quickSort(i, high); } 2 3 1 6 7 -2 8 4 4 } private void swap(int i, int j) { int temp = input[i]; input[i] = input[j]; input[j] = temp; } 2 3 1 -2 4 6 8 7 4 2 3 1 -2 4 4 8 7 6 } JAVA CERTIFICATION TRAINING www.edureka.co/java-j2ee-soa-training

  31. Bubble Sort Selection Sort Insertion Sort Quick Sort Merge Sort 4 1 10 -3 12 // Merges two subarrays of arr[] // First subarray is arr[l..m] // Second subarray is arr[m+1..r] void mergeSort(int arr[], int l, int m, int r) { // Find sizes of two subarrays to be merged int n1 = m - l + 1; int n2 = r - m; 4 1 10 -3 12 1 4 10 -3 12 4 1 10 -3 12 /* Create temp arrays */ int L[] = new int [n1]; int R[] = new int [n2]; 1 4 10 -3 12 /*Copy data to temp arrays*/ for (int i=0; i<n1; ++i) L[i] = arr[l + i]; for (int j=0; j<n2; ++j) R[j] = arr[m + 1+ j]; 1 4 10 -3 12 -3 1 4 10 12 JAVA CERTIFICATION TRAINING www.edureka.co/java-j2ee-soa-training

  32. /*Copy data to temp arrays*/ for (int i=0; i<n1; ++i) L[i] = arr[l + i]; for (int j=0; j<n2; ++j) R[j] = arr[m + 1+ j]; Bubble Sort Selection Sort Insertion Sort Quick Sort Merge Sort 4 1 10 -3 12 /* Merge the temp arrays */ // Initial indexes of first and second subarrays int i = 0, j = 0; 4 1 10 -3 12 // Initial index of merged subarry array int k = l; while (i < n1 && j < n2) { if (L[i] <= R[j]) { arr[k] = L[i]; i++; } else { arr[k] = R[j]; j++; } k++; } 1 4 10 -3 12 4 1 10 -3 12 1 4 10 -3 12 1 4 10 -3 12 -3 1 4 10 12 JAVA CERTIFICATION TRAINING www.edureka.co/java-j2ee-soa-training

  33. k++; } Bubble Sort Selection Sort Insertion Sort Quick Sort Merge Sort 4 1 10 -3 12 /* Copy remaining elements of L[] if any */ while (i < n1) { arr[k] = L[i]; i++; k++; } 4 1 10 -3 12 1 4 10 -3 12 /* Copy remaining elements of R[] if any */ while (j < n2) { arr[k] = R[j]; j++; k++; } 4 1 10 -3 12 1 4 10 -3 12 1 4 10 -3 12 } -3 1 4 10 12 JAVA CERTIFICATION TRAINING www.edureka.co/java-j2ee-soa-training

  34. k++; } } Bubble Sort Selection Sort Insertion Sort Quick Sort Merge Sort 4 1 10 -3 12 // Main function that sorts arr[l..r] using // merge() void sort(int arr[], int l, int r) { if (l < r) { // Find the middle point int m = (l+r)/2; 4 1 10 -3 12 1 4 10 -3 12 4 1 10 -3 12 // Sort first and second halves sort(arr, l, m); sort(arr , m+1, r); 1 4 10 -3 12 1 4 10 -3 12 // Merge the sorted halves merge(arr, l, m, r); } -3 1 4 10 12 } JAVA CERTIFICATION TRAINING www.edureka.co/java-j2ee-soa-training

  35. Searching in Arrays

  36. Sorting in Array Java offers various sorting algorithms that helps in putting elements of a list in a certain order Types of Searching Algorithms Linear Search Binary Search JAVA CERTIFICATION TRAINING www.edureka.co/java-j2ee-soa-training

  37. Linear Search Binary Search 4 static int search(int arr[], int n, int x) { for (int i = 0; i < n; i++) { // Return the index of the element if the element // is found if (arr[i] == x) return i; } 1 10 -3 // return -1 if the element is not found return -1; } 12 JAVA CERTIFICATION TRAINING www.edureka.co/java-j2ee-soa-training

  38. Linear Search Binary Search int binarySearch(int arr[], int l, int r, int x) { if (r>=l) { int mid = l + (r - l)/2; // If the element is present at the // middle itself if (arr[mid] == x) return mid; 2 6 7 9 15 26 50 79 83 2 6 7 9 15 26 50 79 83 // If element is smaller than mid, then // it can only be present in left subarray if (arr[mid] > x) return binarySearch(arr, l, mid-1, x); 15 26 50 79 83 // Else the element can only be present // in right subarray return binarySearch(arr, mid+1, r, x); } JAVA CERTIFICATION TRAINING www.edureka.co/java-j2ee-soa-training

More Related