Docsity

Attendance for Week 11, Exercises of Computer Science

Toggle sidebar
bg1
ArrayList<Integer> a=new ArrayList();
ArrayList<Integer> b=new ArrayList();
Array List <ArrayList<Integer>> c =new ArrayList();
c.add(a);
c.add(b);
//multiplication table
//alphabets //Using Arrays List
Alphabets storing them into a 2D ArrayList
import java.util.ArrayList;
public class Alphabet2DArrayList {
public static void main(String[] args) {
ArrayList<ArrayList<Character>> alphabet2D = new ArrayList<>();
for (int i = 0; i < 2; i++) {
ArrayList<Character> row = new ArrayList<>();
for (int j = 0; j < 13; j++) {
char letter = (char) ('A' + (i * 13 + j));
row.add(letter);
}
alphabet2D.add(row);
}
for (ArrayList<Character> row : alphabet2D) {
System.out.println(row);
}
}
}
import java.util.ArrayList;
import java.util.Collections;
public class ArrayListUtils {
// 1. Method to reverse an ArrayList of doubles
public static ArrayList<Double> reverseArrayList(ArrayList<Double> list) {
ArrayList<Double> reversedList = new ArrayList<>(list);
Collections.reverse(reversedList);
return reversedList;
}
pf3
pf4

Partial preview of the text

Download Attendance for Week 11 and more Exercises Computer Science in PDF only on Docsity!

ArrayList a=new ArrayList(); ArrayList b=new ArrayList(); Array List <ArrayList> c =new ArrayList(); c.add(a); c.add(b); //multiplication table //alphabets //Using Arrays List Alphabets storing them into a 2D ArrayList import java.util.ArrayList; public class Alphabet2DArrayList { public static void main(String[] args) { ArrayList<ArrayList> alphabet2D = new ArrayList<>(); for (int i = 0; i < 2; i++) { ArrayList row = new ArrayList<>(); for (int j = 0; j < 13; j++) { char letter = (char) ('A' + (i * 13 + j)); row.add(letter); } alphabet2D.add(row); } for (ArrayList row : alphabet2D) { System.out.println(row); } } } import java.util.ArrayList; import java.util.Collections; public class ArrayListUtils { // 1. Method to reverse an ArrayList of doubles public static ArrayList reverseArrayList(ArrayList list) { ArrayList reversedList = new ArrayList<>(list); Collections.reverse(reversedList); return reversedList; }

// 2. Method to swap two indices in an ArrayList public static void swapIndices(ArrayList list, int index1, int index2) { T temp = list.get(index1); list.set(index1, list.get(index2)); list.set(index2, temp); } // 3. Method to remove an index value from an ArrayList public static void removeAtIndex(ArrayList list, int index) { if (index >= 0 && index < list.size()) { list.remove(index); } } // 4. Method to return the average of an ArrayList of integers public static double averageArrayList(ArrayList list) { if (list.isEmpty()) return 0; int sum = 0; for (int num : list) { sum += num; } return (double) sum / list.size(); } // 5. Method to return the union of two generic ArrayLists public static ArrayList union(ArrayList list1, ArrayList list2) { ArrayList unionList = new ArrayList<>(list1); for (T item : list2) { if (!unionList.contains(item)) { unionList.add(item); } } return unionList; } // 6. Method to check if a string exists in an ArrayList of strings public static boolean stringExists(ArrayList list, String str) { return list.contains(str); } // 7. Method to convert a traditional array of integers to an ArrayList public static ArrayList arrayToArrayList(int[] array) { ArrayList list = new ArrayList<>();

list1.add(2); ArrayList list2 = new ArrayList<>(); list2.add(2); list2.add(3); System.out.println("Union: " + union(list1, list2)); System.out.println("Does list contain 'banana'? " + stringExists(strings, "banana")); int[] traditionalArray = {4, 5}; ArrayList arrayList = arrayToArrayList(new int[]{1, 2, 3}); addArrayToArrayList(arrayList, traditionalArray, 1); System.out.println("After adding traditional array at index 1: " + arrayList); } }