Java 检查整个阵列是否为instanceof

Java 检查整个阵列是否为instanceof,java,swing,generics,Java,Swing,Generics,我想要一个通用的方法,但是我不确定它是否适合这个场景,我也不太熟悉通用的工作原理(如果有人能给我链接一篇好的教程或文章,我将非常感激) 但是,我想创建一个方法来处理JComponents的初始化,如果JComponents数组都是JRadioButtons,则发送到另一个方法 public void initializeComponent(JComponent...components) { if(components[0] instanceof JRadioButton)

我想要一个通用的方法,但是我不确定它是否适合这个场景,我也不太熟悉通用的工作原理(如果有人能给我链接一篇好的教程或文章,我将非常感激)

但是,我想创建一个方法来处理JComponents的初始化,如果JComponents数组都是JRadioButtons,则发送到另一个方法

public void initializeComponent(JComponent...components)
{
     if(components[0] instanceof JRadioButton)
         initializeJRadioButtons(components[]);
}
但是,这只会检查第一个组件是否是JRadioButton,我觉得泛型可以更好地处理这个问题,但是有没有办法检查所有组件是否都是JRadioButton而不循环

比如如果有人这样做

JRadioButton[] radioButtons = new JRadioButton[2];
...
initializeComponent(radioButtons);

不,没有内置的方法来检查数组中的所有条目是否属于特定的子类型


循环它。

不,没有内置的方法来检查数组中的所有条目是否都是特定的子类型


循环。

不,不循环就不能检查它们。考虑任何可以为你做的过程-在某个时候,它仍然需要逐个检查它们,因为它们是不同的对象。 但是,您可以使用方法重载。您可以有一个签名为的方法

public void initializeComponent(JRadioButton...components)
然后您就知道这些组件中的每一个都是单选按钮,因此可以跳过任何检查

然后,您可以使用问题中已有的方法,该方法可能需要检查每个组件以进行正确的初始化

public void initializeComponent(JComponent...components)

不,你不能不循环检查它们。考虑任何可以为你做的过程-在某个时候,它仍然需要逐个检查它们,因为它们是不同的对象。 但是,您可以使用方法重载。您可以有一个签名为的方法

public void initializeComponent(JRadioButton...components)
然后您就知道这些组件中的每一个都是单选按钮,因此可以跳过任何检查

然后,您可以使用问题中已有的方法,该方法可能需要检查每个组件以进行正确的初始化

public void initializeComponent(JComponent...components)

我看不出在组件上循环的问题是什么:

boolean isRadioButtons = true;
for (Component c : components) {
    if (!(c instanceof JRadioButton)) {
        isRadioButtons = false;
        break;
    }
}

我看不出在组件上循环的问题是什么:

boolean isRadioButtons = true;
for (Component c : components) {
    if (!(c instanceof JRadioButton)) {
        isRadioButtons = false;
        break;
    }
}

由于您只想检查传递的数组是否实际上是
JRadioButton[]
数组,因此可以使用以下代码。尽管这是一种黑客行为,但它确实起到了作用:

public static void initializeComponent(JComponent... components) throws ClassNotFoundException {
    // Get the Class instance for an array of type JRadioButton.
    Class<?> clazz = Class.forName("[Ljavax.swing.JRadioButton;");

    if (clazz.isInstance(components)) { 
        System.out.println(true);
        initializeJRadioButtons((JRadioButton[]) components);
    }
}
publicstaticvoidinitializecomponent(JComponent…components)抛出ClassNotFoundException{
//获取JRadioButton类型数组的类实例。
Class clazz=Class.forName(“[Ljavax.swing.JRadioButton;”);
if(clazz.isInstance(components)){
System.out.println(真);
初始化RadioButtons((JRadioButton[])组件;
}
}
该方法使用
JRadioButton
数组的编码来获取该类型数组的
实例。有关详细信息,请参阅


如果
组件
JRadioButton[]
的一个实例,那么您将得到
true
。请记住,正如我已经说过的,这只是一个黑客攻击。在我看来,重载是您的最佳选择。

因为您只想检查传递的数组是否实际上是
JRadioButton[]
array或not,您可以使用以下代码。虽然这是一种黑客攻击,但可以完成此任务:

public static void initializeComponent(JComponent... components) throws ClassNotFoundException {
    // Get the Class instance for an array of type JRadioButton.
    Class<?> clazz = Class.forName("[Ljavax.swing.JRadioButton;");

    if (clazz.isInstance(components)) { 
        System.out.println(true);
        initializeJRadioButtons((JRadioButton[]) components);
    }
}
publicstaticvoidinitializecomponent(JComponent…components)抛出ClassNotFoundException{
//获取JRadioButton类型数组的类实例。
Class clazz=Class.forName(“[Ljavax.swing.JRadioButton;”);
if(clazz.isInstance(components)){
System.out.println(真);
初始化RadioButtons((JRadioButton[])组件;
}
}
该方法使用
JRadioButton
数组的编码来获取该类型数组的
实例。有关详细信息,请参阅


如果
组件
JRadioButton[]的实例
,然后您将得到
true
。请记住,正如我已经说过的,这只是一种攻击。在我看来,重载是您的最佳选择。

也许您可以通过在组件上循环来简化任务,然后执行
instanceof
测试并委托给
initializeRadioButton
(无复数)对于每个组件?您需要立即初始化组件数组吗?我不知道泛型如何帮助您做到这一点。@subwrajyotimajumder这不会起作用。可能是因为所有条目都不是
JRadioButton
没有任何不使用循环的特殊原因吗?@Otanan是的,现在也知道了。所以您有专门的方法来初始化radio按钮和其他东西,但你想把它作为所有这些的输入方法吗?为什么?还有,你试过重载吗?
init(Foo…foos)
init(Bar…Bar)
,等等?也许您可以通过在组件上循环来简化任务,然后执行
instanceof
测试并委托给
initializeRadioButton
(无复数)对于每个组件?您需要立即初始化组件数组吗?我不知道泛型如何帮助您做到这一点。@subwrajyotimajumder这不会起作用。可能是因为所有条目都不是
JRadioButton
没有任何不使用循环的特殊原因吗?@Otanan是的,现在也知道了。所以您有专门的方法来初始化radio按钮,还有其他一些东西,但是你想把它作为所有这些东西的输入方法吗?为什么?还有,你试过重载吗?
init(Foo…foos)
init(Bar…Bar)
,等等?这肯定是我最终可能使用的,但是,会列出radioButtons=new ArrayList(组件);工作?或者如果components数组包含非JRadioButton,我会因为尝试将非JRadioButton添加到列表中而引发异常吗?您会得到语法错误,因为您是
components
变量类型不是
JRadioButton
或它的子类型(而是超类型)。它会在com上失败