There are three arrayList:

private ArrayList<Clothes> clothes = new ArrayList<>(); private ArrayList<Computer> computers = new ArrayList<>(); private ArrayList<Smartphone> smartphones = new ArrayList<>(); 

there is a switch which, depending on the selected one, passes to the arraList method:

 switch (checkInt()) { case 1: searchName(clothes); break; case 2: searchName(computers); break; case 3: searchName(smartphones); break;} 

actually the question: how should the searchName method accept arrayList

 searchName(???????????????){ } 
  • ArrayList<Product> where Product is the parent class for all listed - Alexey Shimansky
  • The interface is needed for such cases - Kirill

2 answers 2

Maksim,

Immediately struck that your type is a variable class, not an interface. Best practice advises to use just the same interface, for example:

 private List<Clothes> clothes = new ArrayList<>(); private List<Computer> computers = new ArrayList<>(); private List<Smartphone> smartphones = new ArrayList<>(); 

Regarding your question, you can use the generic methodology:

 public <T> void searchName(List<T> list) 

The question is not known whether the classes Clothes, Computer and Smartphone are the heirs of some other class or interface. If they were, for example:

 interface Goods { int price(); int name(); int amount(); } class Clothes implements Goods { ... } class Computer implements Goods { ... } 

In this form, the code would become more concise: Initialization:

 private static List<Goods> clothes = new ArrayList<>(); private static List<Goods> computers = new ArrayList<>(); 

Filling:

 clothes.add(new Clothes()); computers.add(new Computers()); 

The method would remain unified:

 public void searchName(List<Goods> list) { for (Goods entity : list) { System.out.println(entity.price()); } } 

And the method call is the same for each of the collections:

 searchName(clothes); searchName(computers); 

In the worst case, in the version with generics, it will be possible to determine which particular object came using the instanceof operator.

  • Thank you, this is not yet clear to me, but I will read. The listed classes are the heirs of one class, which in turn is the heir from the main class. - Maxim Fomichev
  • In this case, it is logical to do: private List <Parent> clothes = new ArrayList <> (); private List <Parent> computers = new ArrayList <> (); Method: searchName (List <Parent> list) Call: searchName (clothes); searchName (computers); - Mikita Berazouski
  • Thank! It’s not completely clear, but it works) I think I'll figure it out in the process! - Maxim Fomichev
  • And best practices says that elements of one type (interface) can be stored in one container and many more things. - Kirill
  • Well, it already depends on how a person wants to build his structural composition. I still think that the best practices are how to "do", and not how you can. But in principle, yes, you can combine ... =) - Mikita Berazouski

Submit like this:

 searchName(ArrayList<Object> objects){ } 

But you need to organize a search in these arrays? Then you need to define one interface for all these classes. Let's call it, for example Searchable . We describe the getName () function in it. then it will be possible to pass parameters to the function like this:

 Searchable searchName(ArrayList<Searchable> searchableList){ for (Searchable element in searchableList){ if(element.getName() == "My some name") return element; } }