无法将ArrayList <string>转换为ArrayList <java.lang.string> </java.lang.string> </string>

时间:2012-08-27 20:48:18

标签: java swing arraylist jcombobox

我有一个奇怪的(在我看来)问题。我目前正在为自定义渲染器我需要的一些自定义功能制作一个自定义JComboBoxModel我还不确定如何处理,我将在稍后发布。

无论如何,正如标题所示我得到了一些类型错误。

这是类(当前)代码:

package miscclasses;
import java.util.ArrayList;
import javax.swing.AbstractListModel;

public class CustomComboBoxModel<String> extends AbstractListModel<String> {
    /**
     * Contains a list of row indexes that shouldn't be displayed.
     */
    private ArrayList<String> alreadySelectedIndexes;
    /**
     * The comboBoxes selected index.
     */
    private int selectedIndex=-1;
    /**
     * Contains a list of values in this model.
     */
    private ArrayList<String> vals;
    /**
     * Creates an empty CustomComboBoxModel.
     */
    public CustomComboBoxModel() {
        this.alreadySelectedIndexes=new ArrayList<>();
        this.vals = new ArrayList<>();
    }
    /**Creates a CustomComboBoxModel with the values passed in.
     * 
     * @param values the row values.
     */
    public CustomComboBoxModel(ArrayList<String> values) {
        this();
        this.vals.addAll(values);
    }

    public CustomComboBoxModel(ArrayList<String> values, ArrayList<String> alreadySelected) {
        this(values);
        this.alreadySelectedIndexes.addAll(alreadySelected);
    }

    public CustomComboBoxModel(ArrayList<String> values, ArrayList<String> alreadySelected, int selectedIndex) {
        this(values, alreadySelected);
        this.selectedIndex=selectedIndex;
    }

    public CustomComboBoxModel(ArrayList<String> values, ArrayList<String> alreadySelected, String selectedValue) {
        this(values, alreadySelected);
        if(!this.vals.isEmpty()) {
            //this.selectedIndex = Misc.containsI(this.vals, selectedValue);
        }
    }

    @Override
    public int getSize() {
        return this.vals.size();
    }

    @Override
    public String getElementAt(int i) {
        return this.vals.get(i);
    }

    public boolean isRowSelected(int i) {
        return ((!this.alreadySelectedIndexes.contains(Integer.toString(i)))?false:true);
    }
}

我收到的错误是在第55行,我尝试获取所选的值索引。这是构造函数中的注释行,它接受两个ArrayList列表和一个String值。该错误指出“没有合适的方法用于containsI(java.util.ArrayList,String)方法miscclasses.Misc.containsI(java.util.ArrayList,java.lang.String)不适用”。

据我所知,String和java.lang.String完全相同。因此,我似乎无法弄清楚可能导致这个问题的原因,并且我认为我会问问那些比我更了解Code-fu的人。

作为参考,Misc.containsI(ArrayList,String)代码在这里:

 /**
 * Finds the index of value Target. Returns -1 if this ArrayList doesn't contain Target.
 * @param a         ArrayList of Strings a.
 * @param target    Value to find.
 * @return          Index of target if found; Else returns -1.
 */
public static int containsI(ArrayList<String> a, String target) {
    for (int i = 0; i < a.size(); i++)
    {
        if(a.get(i).equalsIgnoreCase(target))
            return i;
    }
    return -1;
}

我也很奇怪在我的isRowSelected(int i)函数中收到以下警告: “对java.util.Collection.contains的可疑调用:预期类型String,实际类型String”。

同样,我不明白为什么我会收到警告。它似乎是完全有效的代码,我以前做过类似的事情。任何帮助这个奇怪的警告和错误,将不胜感激。

编辑:更改声明如下:

public class CustomComboBoxModel extends AbstractListModel<String>

而不是:

public class CustomComboBoxModel<String> extends AbstractListModel<String>

似乎已经摆脱了警告和错误。我不明白为什么,因为我有一个自定义JList模式声明:

public class CustomListModel<String> extends javax.swing.DefaultListModel<String>

在同一个包中没有错误。

1 个答案:

答案 0 :(得分:11)

在此行中,您已声明了一个名为String的类型参数:

public class CustomComboBoxModel<String> extends AbstractListModel<String> {
                                 ^^^^^^

当您稍后引用String时,编译器认为您的意思是类型参数,而不是java.lang.String类。

此处的解决方案是删除type参数,因为您不需要类型参数。


以下简化示例给出了类似的错误消息:

class Foo<String>
{
    String s = ""; // error: incompatible types
}

完整的错误消息是:

Main.java:3: incompatible types
found   : java.lang.String
required: String
    String s = "";
               ^

在线查看:ideone