无法比较的对象类型 - >对象到字符串? [重复]

时间:2022-08-28 16:22:21

This question already has an answer here:

这个问题在这里已有答案:

The method I have is supposed to return a String [] so i used toArray method. But I get error regarding object cannot be converted to strings. I have initialized the list as String as well and am unable to figure out the error that I am getting. Everywhere I read, they say initialize as String and I have already done that. how can I fix it??

我有的方法应该返回一个String [],所以我用toArray方法。但我得到关于对象无法转换为字符串的错误。我已经将列表初始化为String,但我无法弄清楚我得到的错误。我读到的每个地方,他们都说初始化为String,我已经做过了。我该怎么办呢?

 ArrayList<String> c = new ArrayList<String>(Arrays.asList(a));
.......(job done)
return c.toArray();

--The entire code:

- 整个代码:

public static String[] anagrams(String [] a) {
        ArrayList<String> b = new ArrayList<String>(Arrays.asList(a));
        ArrayList<String> c = new ArrayList<String>(Arrays.asList(a));
        int l=a.length;
        int i,j;
        for (i=0;i<l;i++) {
            for (j=i+1;j<l;j++) {
                if (check(b.get(i),b.get(j))){
                    if (c.contains(b.get(j)))
                        c.remove(j);
                }    
            }
        }
        return c.toArray();
}

2 个解决方案

#1


1  

Tryy this

 return c.toArray(new String[c.size()]);

This basically initializes size of the array

这基本上初始化了数组的大小

#2


1  

There are two toArray methods in an ArrayList. From the docs:

ArrayList中有两个toArray方法。来自文档:

Object[] toArray()
Returns an array containing all of the elements in this list in proper sequence (from first to last element).

<T> T[] toArray(T[] a)
Returns an array containing all of the elements in this list in proper sequence (from first to last element); the runtime type of the returned array is that of the specified array.

Right now you are using the first version, which returns an Object array. Since you want a String array, not an Object array, you must use the second version:

现在您正在使用第一个版本,它返回一个Object数组。由于您需要String数组而不是Object数组,因此必须使用第二个版本:

return c.toArray(new String[0]);

The array parameter is needed so ArrayList knows which type to return. If you provide an empty array, ArrayList will allocate a new array for the desired type. However you can also provide an array that is big enough for all elements of the list, then ArrayList will use that array instead of initializing a new one:

需要数组参数,以便ArrayList知道要返回的类型。如果提供空数组,ArrayList将为所需类型分配新数组。但是,您也可以为列表的所有元素提供足够大的数组,然后ArrayList将使用该数组而不是初始化新数组:

return c.toArray(new String[c.size()]);

#1


1  

Tryy this

 return c.toArray(new String[c.size()]);

This basically initializes size of the array

这基本上初始化了数组的大小

#2


1  

There are two toArray methods in an ArrayList. From the docs:

ArrayList中有两个toArray方法。来自文档:

Object[] toArray()
Returns an array containing all of the elements in this list in proper sequence (from first to last element).

<T> T[] toArray(T[] a)
Returns an array containing all of the elements in this list in proper sequence (from first to last element); the runtime type of the returned array is that of the specified array.

Right now you are using the first version, which returns an Object array. Since you want a String array, not an Object array, you must use the second version:

现在您正在使用第一个版本,它返回一个Object数组。由于您需要String数组而不是Object数组,因此必须使用第二个版本:

return c.toArray(new String[0]);

The array parameter is needed so ArrayList knows which type to return. If you provide an empty array, ArrayList will allocate a new array for the desired type. However you can also provide an array that is big enough for all elements of the list, then ArrayList will use that array instead of initializing a new one:

需要数组参数,以便ArrayList知道要返回的类型。如果提供空数组,ArrayList将为所需类型分配新数组。但是,您也可以为列表的所有元素提供足够大的数组,然后ArrayList将使用该数组而不是初始化新数组:

return c.toArray(new String[c.size()]);