如何通过ArrayList搜索子类及商店指定类型产品展示实现方案
嘿,我来帮你搞定这两个问题!先从基础的ArrayList搜索子类讲起,再结合你的商店系统场景给出具体的实现方案~
一、如何通过ArrayList搜索子类?
在Java中,要从存储父类对象的ArrayList里筛选出特定子类的实例,有两种常用方法,各有适用场景:
方法1:使用instanceof关键字
这是最直观的方式,它会判断对象是否是目标类或其子类的实例,适合需要包含子类的子类的场景。
示例代码:
import java.util.ArrayList; import java.util.List; // 父类 class Animal {} // 子类 class Cat extends Animal {} // 子类的子类(如果有的话) class PersianCat extends Cat {} public class SubclassSearchDemo { public static void main(String[] args) { List<Animal> animalList = new ArrayList<>(); animalList.add(new Cat()); animalList.add(new PersianCat()); animalList.add(new Animal()); // 筛选所有Cat类型(包括PersianCat) List<Cat> catList = new ArrayList<>(); for (Animal animal : animalList) { if (animal instanceof Cat) { catList.add((Cat) animal); } } System.out.println("找到的Cat及子类数量:" + catList.size()); // 输出2 } }
方法2:使用getClass()精确匹配
如果需要严格匹配某个子类(不包含它的子类),可以用getClass().equals(TargetSubclass.class),因为getClass()返回的是对象实际所属的类,不会包含继承关系。
示例代码:
// 沿用上面的Animal、Cat、PersianCat类 public class ExactSubclassSearch { public static void main(String[] args) { List<Animal> animalList = new ArrayList<>(); animalList.add(new Cat()); animalList.add(new PersianCat()); animalList.add(new Animal()); // 仅筛选严格的Cat类型(排除PersianCat) List<Cat> exactCatList = new ArrayList<>(); for (Animal animal : animalList) { if (animal.getClass().equals(Cat.class)) { exactCatList.add((Cat) animal); } } System.out.println("精确匹配的Cat数量:" + exactCatList.size()); // 输出1 } }
二、商店系统:展示用户指定类型的产品
针对你的场景,我们可以结合上面的方法,通过遍历两个ArrayList(Peripherals和Gadgets),筛选出用户指定类型的产品。下面是完整的实现思路和代码:
实现步骤
- 定义父类(Peripherals、Gadgets)和对应的产品子类(Processor、Screen、Phone等);
- 初始化两个集合分别存储两类产品;
- 获取用户输入的目标产品类型;
- 遍历两个集合,用
isInstance()动态判断元素是否匹配目标类型; - 输出筛选结果。
完整代码
import java.util.ArrayList; import java.util.List; import java.util.Scanner; // 外设父类 class Peripherals {} // 处理器子类 class Processor extends Peripherals {} // 屏幕子类 class Screen extends Peripherals {} // 数码产品父类 class Gadgets {} // 手机子类 class Phone extends Gadgets {} // 平板子类 class Tablet extends Gadgets {} public class StoreProductDisplay { public static void main(String[] args) { // 初始化产品集合 List<Peripherals> peripherals = new ArrayList<>(); peripherals.add(new Processor()); peripherals.add(new Screen()); peripherals.add(new Processor()); peripherals.add(new Screen()); List<Gadgets> gadgets = new ArrayList<>(); gadgets.add(new Phone()); gadgets.add(new Tablet()); gadgets.add(new Phone()); // 获取用户输入 Scanner scanner = new Scanner(System.in); System.out.print("请输入要查找的产品类型(比如Processor、Screen、Phone、Tablet):"); String targetTypeName = scanner.nextLine().trim(); // 映射用户输入到对应的Class对象(避免反射风险) Class<?> targetClass = null; switch (targetTypeName) { case "Processor": targetClass = Processor.class; break; case "Screen": targetClass = Screen.class; break; case "Phone": targetClass = Phone.class; break; case "Tablet": targetClass = Tablet.class; break; default: System.out.println("抱歉,没有这个类型的产品!"); scanner.close(); return; } // 筛选匹配的产品 List<Object> matchedProducts = new ArrayList<>(); // 遍历外设集合 for (Peripherals p : peripherals) { if (targetClass.isInstance(p)) { matchedProducts.add(p); } } // 遍历数码产品集合 for (Gadgets g : gadgets) { if (targetClass.isInstance(g)) { matchedProducts.add(g); } } // 展示结果 if (matchedProducts.isEmpty()) { System.out.println("未找到任何" + targetTypeName + "类型的产品"); } else { System.out.println("共找到" + matchedProducts.size() + "个" + targetTypeName + "类型的产品:"); for (Object product : matchedProducts) { System.out.println("- " + product.getClass().getSimpleName()); } } scanner.close(); } }
关键说明
- 用
targetClass.isInstance(obj)替代硬编码的instanceof,可以根据用户输入动态切换目标类型,更灵活; - 采用switch映射输入到Class对象,比反射(
Class.forName())更安全,避免用户输入无效类名导致的异常; - 分别遍历两个集合是因为它们的父类不同,这样代码逻辑更清晰,也能避免类型转换问题。
内容的提问来源于stack exchange,提问作者Λολος
相关产品推荐
相关产品推荐

