Array Lists
hacks for array lists
import java.util.ArrayList; // Import the ArrayList class
ArrayList<String> stringList = new ArrayList<>();
stringList.add("Apple");
stringList.add("Banana");
stringList.add("Cherry");
stringList.add("Date");
//print out original size
System.out.println(stringList.size());
//add new element "Fig"
stringList.add("Fig");
//print out size
System.out.println(stringList.size());
//add new element "Grape" at index of 2
stringList.add(2,"Grape");
import java.util.ArrayList; // Import the ArrayList class
class ArrayListHacks {
private int findSum(ArrayList<Integer> values) {
int sum = 0;
for (Integer number : values){
sum += number.intValue();
}
return sum;
}
public static void main(String[] args) {
ArrayList<Integer> nums = new ArrayList<>();
nums.add(0);
nums.add(1);
nums.add(2);
nums.add(3);
nums.add(5);
nums.add(8);
ArrayListHacks hacks = new ArrayListHacks();
System.out.println(hacks.findSum(nums));
}
}
ArrayListHacks.main(null);
19
import java.util.ArrayList; // Import the ArrayList class
ArrayList<Double> weights = new ArrayList<>();
weights.add(4.5);
weights.add(2.1);
weights.add(-5.0);
weights.add(-1.9);
weights.add(-3.7);
weights.add(4.7);
ArrayList<Double> step(ArrayList<Double> array1){
for(int i = 1; i < array1.size(); i++){
if(array1.get(i-1) > array1.get(i)){
Double temp = array1.get(i-1);
array1.remove(i-1);
array1.add(i,temp);
}
}
return array1;
}
ArrayList<Double> sortArray(ArrayList<Double> array){
boolean complete = false;
while (!complete){
for(int i = 1; i < array.size(); i++){
if(array.get(i-1) > array.get(i)){
array = step(array);
break;
}
else if (i == array.size() - 1){
complete = true;
}
}
}
return array;
}
weights = sortArray(weights);
System.out.println(Arrays.toString(weights.toArray()));
[-5.0, -3.7, -1.9, 2.1, 4.5, 4.7]
You could
userData = null; //or new ArrayList<>();
ArrayList myList = new ArrayList();
myList.add("Hello");
myList.add("world,");
myList.add("my");
myList.add("favorite");
myList.add("color");
myList.add("is");
myList.add("red");
System.out.println(Arrays.toString(myList.toArray()));
ArrayList myList = new ArrayList();
myList.add("Hello");
myList.add("world,");
myList.add("my");
myList.add("favorite");
myList.add("color");
myList.add("is");
myList.add("red");
myList.set(1, "Del Norte");
System.out.println(Arrays.toString(myList.toArray()));
[Hello, Del Norte, my, favorite, color, is, red]
ArrayList myList = new ArrayList();
myList.add("Hello");
myList.add("world,");
myList.add("my");
myList.add("favorite");
myList.add("color");
myList.add("is");
myList.add("red");
myList.set(1, "Del Norte");
myList.remove(1);
System.out.println(Arrays.toString(myList.toArray()));
[Hello, my, favorite, color, is, red]
ArrayList myList = new ArrayList();
myList.add("Hello");
myList.add("world,");
myList.add("my");
myList.add("favorite");
myList.add("color");
myList.add("is");
myList.add("red");
System.out.println(myList.contains("Hello"));
true
ArrayList myList = new ArrayList();
myList.add("Hello");
myList.add("world,");
myList.add("my");
myList.add("favorite");
myList.add("color");
myList.add("is");
myList.add("red");
for(Object value: myList){
System.out.println(value);
}
Hello
world,
my
favorite
color
is
red
ArrayList myList = new ArrayList();
myList.add(2);
myList.add(4);
myList.add(-3);
myList.add(7);
myList.add(1);
myList.add(5);
myList.add(3);
Collections.sort(myList);
System.out.println(Arrays.toString(myList.toArray()));
[-3, 1, 2, 3, 4, 5, 7]