Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cannot convert ArrayList<String> to ArrayList<java.lang.String>

I have an odd (in my mind) problem. I am currently making a Custom JComboBoxModel for some custom functionality I need from a Custom Renderer I am not yet certain how to deal with, I will post regarding that later.

Regardless, as the Title suggested I am getting some Type errors.

Here is the classes (current) code:

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);
    }
}

The error I am receiving is on line 55 where I attempt to get the selected values index. This is the commented line in the constructor that accepts two ArrayList lists and a String value. The error states that there is "no suitable method for containsI(java.util.ArrayList,String) method miscclasses.Misc.containsI(java.util.ArrayList, java.lang.String) is not applicable".

So far as I know String and java.lang.String are the exact same thing. As such I cannot seem to figure out what could cause this issue, and thought I would come to ask those more knowledgeable in the ways of Code-fu than I.

For reference, the Misc.containsI(ArrayList,String) code is here:

 /**
 * 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;
}

I am also strangely receiving the following warning in my isRowSelected(int i) function: "Suspcious call to java.util.Collection.contains: Expected type String, actual type String".

Again, I don't understand why I am getting warning. It seems to be entirely valid code, and I have done similar before. Any help with this odd warning and error, would be much appreciated.

EDIT: Changing the Declaration so it reads:

public class CustomComboBoxModel extends AbstractListModel<String>

instead of:

public class CustomComboBoxModel<String> extends AbstractListModel<String>

Appears to have gotten rid of the warning and error. I do not understand why however, as I have a Custom JList mode declared as such:

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

In the same package with no errors.

like image 353
Legowaffles Avatar asked Dec 20 '22 17:12

Legowaffles


1 Answers

On this line you have declared a type parameter called String:

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

When you later refer to String the compiler thinks you mean the type parameter, not the java.lang.String class.

The solution here is to remove the type parameter, since you do not need a type parameter.


The following simplified example gives a similar error message:

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

The full error message is:

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

See it online: ideone

like image 133
Mark Byers Avatar answered Dec 24 '22 00:12

Mark Byers