創建一個類對象列表以將它們與另一個類對象進行比較 (creating a list of class objects to compare them with another class object)


問題描述

創建一個類對象列表以將它們與另一個類對象進行比較 (creating a list of class objects to compare them with another class object)

首先,對不起,這個問題可能已經回答了,但我的搜索並沒有真正告訴我任何事情,而且我也不知道我在搜索什麼。

基本上,我需要創建一個類對象的List(只存儲類/不存儲初始化的對象 ‑ 真的不知道如何解釋)並將它們與另一個類對象進行比較(ala instanceof </代碼>)。但我不知道該怎麼做。

一個例子是:

我想這樣做:

if(something1 instanceof something2)

帶有一個List 的“something1's”,以便能夠像這樣遍歷它:

for(int i= 0 ; i <= list.size; i++) 
{
  if (list.get(i) instanceof something2)
    doSomething();
}


參考解法

方法 1:

You can compare classes using equals() method, this method will check if the class is exactly the same.

So you first put classes in a list (assuming you have classes Foo and Bar and a list is List<Class>):

list.add(Foo.class);

And then you iterate over it and do comparison like this:

if (list.get(i).equals(Bar.class))
    doSomething();

方法 2:

Class class provides API to do instance of like check:

Class numClass = Number.class;
List<Class> classes = Arrays.asList(Integer.class);
...
if (numClass.isAssignableFrom(classes.get(i))) {
    doSomething();
}

方法 3:

Using generic lists and class Class:

List<Class> classes = new ArrayList<Class>();
classes.add(Integer.class);
classes.add(String.class);
classes.add(MyClass.class);
//...

for (Class myClass : classes) {
    if (myClass.equals(MyOtherClass.class)) {
        doSomething(); 
    }
}

方法 4:

List of classes can be declared as List<Class> list, e.g.

List<Class> classes = Arrays.asList(String.class, Number.class);
Class testedClass = Integer.class;

If you want to test the exact classes equality use equals.

for (Class clazz : list) {
    if (clazz.equals(testedClass)) {
        doSomething();
    }
}

Or even shorter

if (list.contains(testedClass)) {
    doSomething();
}

But if you need an analogue of instanceof then use isAssignableFrom

for (Class clazz : list) {
    if (clazz.isAssignableFrom(testedClass)) {
        doSomething();
    }
}

isAssignableFrom means that clazz and testedClass are in the same hierarchy and testedClass extends or equals to class

(by Alex EpvpKrzysztof KrasońhoazmarcellorvalleEvgeny)

參考文件

  1. creating a list of class objects to compare them with another class object (CC BY‑SA 2.5/3.0/4.0)

#java #forge #loops #list #instanceof






相關問題

電子郵件地址中帶有 + 字符的 Java 郵件 (Java mail with + character in email address)

如何快速原型化 Java 代碼? (How to quickly prototype Java code?)

如何使用 Maven 在目標(SVN-)服務器上創建 Javadoc? (How to create Javadoc on the target (SVN-) server using Maven?)

為什麼檢查二叉樹有效性的解決方案不起作用? (Why the solution for checking the validity of binary tree is not working?)

Selenium webdriver通過第一個數字找到texy (Selenium webdriver find texy by first digits)

setOnClickListener 沒有在圖像視圖上被調用 (setOnClickListener is not getting called on image view)

繪製多邊形:找不到錯誤 (Drawing Polygon : unable to find error)

半透明 JButton:對像出現在背景中 (Semi-Transparent JButton: Objects appear in Background)

比較同一數組的元素 (Compare elements of the same array)

Java 屏幕截圖小程序 (Java screen capture applet)

Minecraft 1.8.9 Forge Modding 的Java 開發工具包,需要什麼JDK/JRE,代碼是否正確? (Java Development Kit with Minecraft 1.8.9 Forge Modding, What JDK/JRE Is Needed, Is Code Correct?)

java while (resultset.next()) 不返回同一列中的所有數據 (java while (resultset.next()) does not return all data in the same column)







留言討論