我知道有些人已经问过重复项,我已经查看了所有这些答案,但似乎没有人帮助我/我仍然无法编译。我的代码中是否有一些我不知道的东西?帮助(编程新手!)
我需要编写一个方法,该方法接受一个参数,一个String值数组,并返回一个没有重复值且不需要对原始数组进行排序的新数组。例如
String [] abc = {"A", "C", "C", "B", "A", "C", "B", "B", "A"};
String [] new = removeDuplicates (abc);
for (int i = 0; i < new.length; i++)
{
System.out.print (new[i] + " ");
}
System.out.println();
输出应为:A C B
这是我的代码:
import java.util.*;
class Duplication
{
public static void main (String [] args)
{
String [] values = {"A", "C", "C", "B", "A", "C", "B", "B", "A"};
String [] newList = removeDuplicates (values);
}
private static String removeDuplicates (String [] originalArray)
{
for (int i = 0; i < newList.length; i++)
{
System.out.print (newList[i] + " ");
}
System.out.println();
String noDuplicates = originalArray.length;
for (int d = 0; d < noDuplicates; d++)
{
for (int e = d + 1; e < noDuplicates; e++)
{
if(originalArray[d] == originalArray[e])
{
originalArray[e] = originalArray[noDuplicates-1];
noDuplicates--;
e--;
}
}
}
}
}
六个错误:
Duplication.java:9: error: incompatible types
String [] newList = removeDuplicates (values);
^
required: String[]
found: String
Duplication.java:14: error: cannot find symbol
for (int i = 0; i < newList.length; i++)
^
symbol: variable newList
location: class Duplicates
Duplication.java:16: error: cannot find symbol
System.out.print (newList[i] + " ");
^
symbol: variable newList
location: class Duplicates
Duplication.java:21: error: incompatible types
String noDuplicates = originalArray.length;
^
required: String
found: int
Duplication.java:32: error: bad operand types for binary operator '-'
originalArray[e] = originalArray[noDuplicates-1];
^
first type: String
second type: int
Duplication.java:35: error: bad operand type String for unary operator '--'
noDuplicates--;
^
答案 0 :(得分:1)
这在java中无效:
public static void main (String [] args)
String [] values = {"A", "C", "C", "B", "A", "C", "B", "B", "A"};
String [] newList = removeDuplicates (values);
你必须添加 {} 来定义静态main方法的范围:
public static void main (String [] args){
String[] values = {"A", "C", "C", "B", "A", "C", "B", "B", "A"};
String[] newList = removeDuplicates (values);
}
答案 1 :(得分:1)
1)你的方法removeDuplicates()返回一个String。在主块中,您需要一个String数组。
2)范围变量newList在主块内。因此,此方法在removeDuplicates
方法中不可见3)“originalArray.length”返回“int”,而不是String。
答案 2 :(得分:1)
您应该阅读错误消息。
Duplication.java:9: error: incompatible types
String [] newList = removeDuplicates (values);
此函数返回String
不是String []
private static String removeDuplicates (String [] originalArray)
private static String [] removeDuplicates (String [] originalArray)
您应该将其声明为
newList
您应该在函数范围内声明Duplication.java:14: error: cannot find symbol
for (int i = 0; i < newList.length; i++)
以使用它,这会导致
int noDuplicates = originalArray.length;
长度是整数,应按如下方式处理:
SELECT person_name
FROM person
WHERE person_id IN
(SELECT Person_Person_Id
FROM customer);
仔细阅读编译错误,它们可以解决所有问题。
答案 3 :(得分:0)
这就是你需要的所有代码。 您可以在此处阅读这些馆藏https://docs.oracle.com/javase/tutorial/collections/
import java.util.*;
class Duplication
{
static String [] values = {"A", "C", "C", "B", "A", "C", "B", "B", "A"};
public static void main (String [] args){
System.out.println(removeDuplicates(values));
}
static Set removeDuplicates(String [] originalArray)
{
List arList = new ArrayList<String>(Arrays.asList(values));
return new LinkedHashSet<String>(arList);
}
}