重复的元素,我知道有些人问重复已经,我已经通过所有这些问题的答案,但看着似乎没有帮我/我仍然不能得到它来编译。我的代码中有什么明显的我失踪?帮助(新的节目!)爪哇 - 错误去除阵列
我需要写它有一个参数,字符串值的数组,并返回没有重复值,无需责令原数组的新阵列的方法。例如
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();
的输出中应该是:ACB
这里是我的代码:
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--;
^
请重新命名新到别的阵列.. –
两个问题:(1)哪里是你''{'后公共静态无效main'? (2)你是否正在尝试一种方法内的方法?因为这是不允许的。 –
1.'new'是一个保留字,Java中没有变量可以使用该名称。 2。在另一个方法中定义一个方法是不正确的。 –