|
| 1 | +package com.baeldung.array; |
| 2 | + |
| 3 | +import java.util.ArrayList; |
| 4 | +import java.util.Arrays; |
| 5 | +import java.util.Comparator; |
| 6 | +import java.util.List; |
| 7 | +import java.util.stream.IntStream; |
| 8 | +import java.util.stream.Stream; |
| 9 | + |
| 10 | +public class ArrayReferenceGuide { |
| 11 | + |
| 12 | + public static void main(String[] args) { |
| 13 | + declaration(); |
| 14 | + |
| 15 | + initialization(); |
| 16 | + |
| 17 | + access(); |
| 18 | + |
| 19 | + iterating(); |
| 20 | + |
| 21 | + varargs(); |
| 22 | + |
| 23 | + transformIntoList(); |
| 24 | + |
| 25 | + transformIntoStream(); |
| 26 | + |
| 27 | + sort(); |
| 28 | + |
| 29 | + search(); |
| 30 | + |
| 31 | + merge(); |
| 32 | + } |
| 33 | + |
| 34 | + private static void declaration() { |
| 35 | + int[] anArray; |
| 36 | + int anotherArray[]; |
| 37 | + } |
| 38 | + |
| 39 | + private static void initialization() { |
| 40 | + int[] anArray = new int[10]; |
| 41 | + anArray[0] = 10; |
| 42 | + anArray[5] = 4; |
| 43 | + |
| 44 | + int[] anotherArray = new int[] {1, 2, 3, 4, 5}; |
| 45 | + } |
| 46 | + |
| 47 | + private static void access() { |
| 48 | + int[] anArray = new int[10]; |
| 49 | + anArray[0] = 10; |
| 50 | + anArray[5] = 4; |
| 51 | + |
| 52 | + System.out.println(anArray[0]); |
| 53 | + } |
| 54 | + |
| 55 | + private static void iterating() { |
| 56 | + int[] anArray = new int[] {1, 2, 3, 4, 5}; |
| 57 | + for (int i = 0; i < anArray.length; i++) { |
| 58 | + System.out.println(anArray[i]); |
| 59 | + } |
| 60 | + |
| 61 | + for (int element : anArray) { |
| 62 | + System.out.println(element); |
| 63 | + } |
| 64 | + } |
| 65 | + |
| 66 | + private static void varargs() { |
| 67 | + String[] groceries = new String[] {"Milk", "Tomato", "Chips"}; |
| 68 | + varargMethod(groceries); |
| 69 | + varargMethod("Milk", "Tomato", "Chips"); |
| 70 | + } |
| 71 | + |
| 72 | + private static void varargMethod(String... varargs) { |
| 73 | + for (String element : varargs) { |
| 74 | + System.out.println(element); |
| 75 | + } |
| 76 | + } |
| 77 | + |
| 78 | + private static void transformIntoList() { |
| 79 | + Integer[] anArray = new Integer[] {1, 2, 3, 4, 5}; |
| 80 | + |
| 81 | + // Naïve implementation |
| 82 | + List<Integer> aList = new ArrayList<>(); // We create an empty list |
| 83 | + for (int element : anArray) { |
| 84 | + // We iterate over array's elements and add them to the list |
| 85 | + aList.add(element); |
| 86 | + } |
| 87 | + |
| 88 | + // Pretty implementation |
| 89 | + aList = Arrays.asList(anArray); |
| 90 | + |
| 91 | + // Drawbacks |
| 92 | + try { |
| 93 | + aList.remove(0); |
| 94 | + } catch (UnsupportedOperationException e) { |
| 95 | + System.out.println(e.getMessage()); |
| 96 | + } |
| 97 | + |
| 98 | + try { |
| 99 | + aList.add(6); |
| 100 | + } catch (UnsupportedOperationException e) { |
| 101 | + System.out.println(e.getMessage()); |
| 102 | + } |
| 103 | + |
| 104 | + int[] anotherArray = new int[] {1, 2, 3, 4, 5}; |
| 105 | +// List<Integer> anotherList = Arrays.asList(anotherArray); |
| 106 | + } |
| 107 | + |
| 108 | + private static void transformIntoStream() { |
| 109 | + int[] anArray = new int[] {1, 2, 3, 4, 5}; |
| 110 | + IntStream aStream = Arrays.stream(anArray); |
| 111 | + |
| 112 | + Integer[] anotherArray = new Integer[] {1, 2, 3, 4, 5}; |
| 113 | + Stream<Integer> anotherStream = Arrays.stream(anotherArray, 2, 4); |
| 114 | + } |
| 115 | + |
| 116 | + private static void sort() { |
| 117 | + int[] anArray = new int[] {5, 2, 1, 4, 8}; |
| 118 | + Arrays.sort(anArray); // anArray is now {1, 2, 4, 5, 8} |
| 119 | + |
| 120 | + Integer[] anotherArray = new Integer[] {5, 2, 1, 4, 8}; |
| 121 | + Arrays.sort(anotherArray); // anArray is now {1, 2, 4, 5, 8} |
| 122 | + |
| 123 | + String[] yetAnotherArray = new String[] {"A", "E", "Z", "B", "C"}; |
| 124 | + Arrays.sort(yetAnotherArray, 1, 3, Comparator.comparing(String::toString).reversed()); // yetAnotherArray is now {"A", "Z", "E", "B", "C"} |
| 125 | + } |
| 126 | + |
| 127 | + private static void search() { |
| 128 | + int[] anArray = new int[] {5, 2, 1, 4, 8}; |
| 129 | + for (int i = 0; i < anArray.length; i++) { |
| 130 | + if (anArray[i] == 4) { |
| 131 | + System.out.println("Found at index " + i); |
| 132 | + break; |
| 133 | + } |
| 134 | + } |
| 135 | + |
| 136 | + Arrays.sort(anArray); |
| 137 | + int index = Arrays.binarySearch(anArray, 4); |
| 138 | + System.out.println("Found at index " + index); |
| 139 | + } |
| 140 | + |
| 141 | + private static void merge() { |
| 142 | + int[] anArray = new int[] {5, 2, 1, 4, 8}; |
| 143 | + int[] anotherArray = new int[] {10, 4, 9, 11, 2}; |
| 144 | + |
| 145 | + int[] resultArray = new int[anArray.length + anotherArray.length]; |
| 146 | + for (int i = 0; i < resultArray.length; i++) { |
| 147 | + resultArray[i] = (i < anArray.length ? anArray[i] : anotherArray[i - anArray.length]); |
| 148 | + } |
| 149 | + for (int element : resultArray) { |
| 150 | + System.out.println(element); |
| 151 | + } |
| 152 | + |
| 153 | + Arrays.setAll(resultArray, i -> (i < anArray.length ? anArray[i] : anotherArray[i - anArray.length])); |
| 154 | + for (int element : resultArray) { |
| 155 | + System.out.println(element); |
| 156 | + } |
| 157 | + } |
| 158 | + |
| 159 | +} |
0 commit comments