[Solved] How to access same method with different objects in java


Simply pass the object of the created ArrayList to the function as paramter.

Here’s a short example for printing the ArrayList:

Assuming you put the code inside a class, leaving a snippet here.
I’ll demonstrate it with Integer examples.

static void printArrayList(ArrayList<Integer> a){
      for(Integer i:a)
           System.out.print(i+" ");
      System.out.println("");

}

//in the main function:
public static void main(String[] args){
      // input driver code
      ArrayList<Integer> a1 = new ArrayList<>();
      ArrayList<Integer> a2 = new ArrayList<>();
      a1.add(1);
      a2.add(10);
      printArrayList(a1);
      printArrayList(a2);
}

For more information on passing parameters – visit official documentation: Function Parameters

For more information on ArrayList – visit official documentation:
ArrayList

4

solved How to access same method with different objects in java