| 1 | class ArraysExample{ |
| 2 | |
| 3 | void cloneArray2(){ |
| 4 | int ai[] = {1, 2, 3, 4, 5}; |
| 5 | |
| 6 | /* copying the reference ai to aic, |
| 7 | * after following assignment, aic |
| 8 | * will point to the same array ai points. |
| 9 | */ |
| 10 | int aic[] = ai; |
| 11 | |
| 12 | aic[2] = -9; |
| 13 | |
| 14 | /* both print statements will print |
| 15 | * the same result because ai, and aic |
| 16 | * are the reference of same array, then no matter |
| 17 | * which reference is being used to update array values. |
| 18 | */ |
| 19 | System.out.println(aic[2]); |
| 20 | System.out.println(ai[2]); |
| 21 | |
| 22 | /* Now illustrating clone(). |
| 23 | * In below assignment ai.clone() creates |
| 24 | * a separate copy of the array in memory |
| 25 | * and then assigns it to aic. Now, both |
| 26 | * ai and aic point to two different arrays |
| 27 | * so changes made to one will not impact the other |
| 28 | */ |
| 29 | System.out.println("---"); //separator |
| 30 | aic = ai.clone(); |
| 31 | aic[1] = -15; |
| 32 | |
| 33 | /* both print statements will print |
| 34 | * the value stored at 1st index in the array |
| 35 | * they point to. |
| 36 | */ |
| 37 | System.out.println(aic[1]); |
| 38 | System.out.println(ai[1]); |
| 39 | } |
| 40 | void cloneArray(){ |
| 41 | int a[] = { 1, 4, 7, 9 }; |
| 42 | |
| 43 | int n = a.length; |
| 44 | int b[] = a.clone(); |
| 45 | |
| 46 | b[0] = 5; |
| 47 | |
| 48 | System.out.println("Original array "); |
| 49 | for (int i = 0; i < n; i++) |
| 50 | System.out.print(a[i] + " "); |
| 51 | |
| 52 | System.out.println("\nCloned Array "); |
| 53 | for (int i = 0; i < b.length; i++) |
| 54 | System.out.print(b[i] + " "); |
| 55 | |
| 56 | } |
| 57 | void searchInArray(){ |
| 58 | //Linear Search7 |
| 59 | int[] arr = {10, 5, 3, 6, 2, 8, 4}; |
| 60 | int x = 3; |
nothing calls this directly
no outgoing calls
no test coverage detected