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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
ArrayList
|
| 2 |
+
|
| 3 |
+
|
| 4 |
+
import java.util.ArrayList;
|
| 5 |
+
|
| 6 |
+
public class Example {
|
| 7 |
+
public static void main(String[] args){
|
| 8 |
+
ArrayList<Double> results = new ArrayList<>();
|
| 9 |
+
|
| 10 |
+
// variable results refers to an object,
|
| 11 |
+
// so list can be manipulated through it
|
| 12 |
+
results.add(2.5);
|
| 13 |
+
results.add(5.75);
|
| 14 |
+
results.add(4.5);
|
| 15 |
+
|
| 16 |
+
for (double result : results) {
|
| 17 |
+
System.out.println(result);
|
| 18 |
+
}
|
| 19 |
+
}
|
| 20 |
+
}
|
| 21 |
+
|
| 22 |
+
Program outputs:
|
| 23 |
+
2.5
|
| 24 |
+
5.75
|
| 25 |
+
4.5
|
| 26 |
+
|
| 27 |
+
|
| 28 |
+
|
| 29 |
+
================================
|
| 30 |
+
|
| 31 |
+
|
| 32 |
+
|
| 33 |
+
Several references to the same object
|
| 34 |
+
|
| 35 |
+
|
| 36 |
+
import java.util.ArrayList;
|
| 37 |
+
|
| 38 |
+
public class Example {
|
| 39 |
+
public static void main(String[] args){
|
| 40 |
+
ArrayList<Integer> numbers = new ArrayList<>();
|
| 41 |
+
ArrayList<Integer> numbersTwo = numbers; // refer to the same 'numbers' list
|
| 42 |
+
|
| 43 |
+
numbers.add(5);
|
| 44 |
+
numbers.add(9);
|
| 45 |
+
numbers.add(15);
|
| 46 |
+
|
| 47 |
+
numbersTwo.add(17);
|
| 48 |
+
numbersTwo.add(21);
|
| 49 |
+
|
| 50 |
+
System.out.println(numbers);
|
| 51 |
+
System.out.println(numbersTwo);
|
| 52 |
+
}
|
| 53 |
+
}
|
| 54 |
+
|
| 55 |
+
|
| 56 |
+
Program outputs:
|
| 57 |
+
[5, 9, 15, 17, 21]
|
| 58 |
+
[5, 9, 15, 17, 21]
|
| 59 |
+
|
| 60 |
+
|
| 61 |
+
|
| 62 |
+
|
| 63 |
+
|
| 64 |
+
|
| 65 |
+
|
| 66 |
+
================================
|
| 67 |
+
|
| 68 |
+
|
| 69 |
+
Easy to match same content for BASIC TYPEs
|
| 70 |
+
|
| 71 |
+
Scanner reader = new Scanner(System.in);
|
| 72 |
+
while (true) {
|
| 73 |
+
System.out.print("Give a number: ");
|
| 74 |
+
int num = Integer.valueOf(reader.nextLine());
|
| 75 |
+
|
| 76 |
+
if (num == 10) {
|
| 77 |
+
System.out.println("You gave a ten!");
|
| 78 |
+
break;
|
| 79 |
+
}
|
| 80 |
+
}
|
| 81 |
+
|
| 82 |
+
Example execution:
|
| 83 |
+
Give a number: 5
|
| 84 |
+
Give a number: 6
|
| 85 |
+
Give a number: 3
|
| 86 |
+
Give a number: 10
|
| 87 |
+
You gave a ten!
|
| 88 |
+
|
| 89 |
+
|
| 90 |
+
|
| 91 |
+
|
| 92 |
+
VS
|
| 93 |
+
|
| 94 |
+
|
| 95 |
+
|
| 96 |
+
|
| 97 |
+
Difficult to match same object for REFERENCE/OBJECT TYPEs
|
| 98 |
+
- match references
|
| 99 |
+
- not matching content
|
| 100 |
+
|
| 101 |
+
|
| 102 |
+
|
| 103 |
+
|
| 104 |
+
ArrayList<Integer> list1 = new ArrayList<>();
|
| 105 |
+
ArrayList<Integer> list2 = new ArrayList<>();
|
| 106 |
+
ArrayList<Integer> list3 = list1;
|
| 107 |
+
|
| 108 |
+
list1.add(1);
|
| 109 |
+
list1.add(2);
|
| 110 |
+
list1.add(3);
|
| 111 |
+
|
| 112 |
+
list2.add(1);
|
| 113 |
+
list2.add(2);
|
| 114 |
+
list2.add(3);
|
| 115 |
+
|
| 116 |
+
System.out.println("list1 == list2? " + (list1 == list2));
|
| 117 |
+
System.out.println("list1 == list3? " + (list1 == list3));
|
| 118 |
+
System.out.println("list2 == list3? " + (list2 == list3));
|
| 119 |
+
|
| 120 |
+
|
| 121 |
+
Program outputs:
|
| 122 |
+
list1 == list2? false // DIFFERENT REFERENCE BUT SAME CONTENT
|
| 123 |
+
list1 == list3? true // SAME REFERENCE, cuz list3 was refererring to list1, and its contents
|
| 124 |
+
list2 == list3? false // DIFFERENT REFERENCE BUT SAME CONTENT
|
| 125 |
+
|
| 126 |
+
|
| 127 |
+
|
| 128 |
+
|
| 129 |
+
|
| 130 |
+
|
| 131 |
+
|
| 132 |
+
|
| 133 |
+
|
| 134 |
+
================================
|
| 135 |
+
|
| 136 |
+
|
| 137 |
+
|
| 138 |
+
The 'equals' operator can be used to compare the SIMILARITY of OBJECTS.
|
| 139 |
+
The operator returns true if the objects have the SAME CONTENT.
|
| 140 |
+
|
| 141 |
+
|
| 142 |
+
|
| 143 |
+
ArrayList<Integer> list1 = new ArrayList<>();
|
| 144 |
+
ArrayList<Integer> list2 = new ArrayList<>();
|
| 145 |
+
ArrayList<Integer> list3 = list1;
|
| 146 |
+
|
| 147 |
+
list1.add(1);
|
| 148 |
+
list1.add(2);
|
| 149 |
+
list1.add(3);
|
| 150 |
+
|
| 151 |
+
list2.add(1);
|
| 152 |
+
list2.add(2);
|
| 153 |
+
list2.add(3);
|
| 154 |
+
|
| 155 |
+
System.out.println("list1.equals(list2) " + (list1.equals(list2)));
|
| 156 |
+
System.out.println("list1.equals(list3) " + (list1.equals(list3)));
|
| 157 |
+
System.out.println("list2.equals(list3) " + (list2.equals(list3)));
|
| 158 |
+
|
| 159 |
+
|
| 160 |
+
Program outputs:
|
| 161 |
+
list1.equals(list2) true
|
| 162 |
+
list1.equals(list3) true
|
| 163 |
+
list2.equals(list3) true
|
| 164 |
+
|
| 165 |
+
|
| 166 |
+
|
| 167 |
+
|
| 168 |
+
|
| 169 |
+
|
| 170 |
+
|
| 171 |
+
|
| 172 |
+
================================
|
| 173 |
+
|
| 174 |
+
|
| 175 |
+
Since STRINGS are Java OBJECTS, their EQUALITY must also be compared using the EQUALS operator, for example
|
| 176 |
+
|
| 177 |
+
Scanner reader = new Scanner(System.in);
|
| 178 |
+
System.out.print("Give a name: ");
|
| 179 |
+
String name = reader.nextLine();
|
| 180 |
+
|
| 181 |
+
|
| 182 |
+
if (name == "Jack Java") {
|
| 183 |
+
System.out.println("nimi == Jack Java"); // THIS DID NOT RUN
|
| 184 |
+
}
|
| 185 |
+
if (name.equals("Jack Java")){
|
| 186 |
+
System.out.println("the name is Jack Java"); // THIS RAN
|
| 187 |
+
}
|
| 188 |
+
|
| 189 |
+
|
| 190 |
+
Program outputs:
|
| 191 |
+
Give a name: Jack Java
|
| 192 |
+
the name is Jack Java
|
| 193 |
+
|
| 194 |
+
|
| 195 |
+
|