import java.util.*; class UseArrayLists7 // Shows adding an integer after all occurrences of an integer // constructively in an ArrayList (improved method) { public static void main(String[] args) throws Exception { Scanner input = new Scanner(System.in); System.out.println("Enter some numbers (all on one line, separated by spaces):"); String line = input.nextLine(); String[] numbers = line.split(" "); ArrayList a = new ArrayList(); for(int i=0; i b=addAfter(a,p,q); System.out.println("\nThe ArrayList created is "+b); System.out.println("The original ArrayList is still "+a); } public static ArrayList addAfter(ArrayList a,Integer m,Integer n) // Adds n after every occurrence of m in a, constructively. // Avoids calling a.get(i) twice. { ArrayList b = new ArrayList(); for(int i=0; i