Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/305.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在java can';不要使用哈希集_Java - Fatal编程技术网

在java can';不要使用哈希集

在java can';不要使用哈希集,java,Java,目前,我在从给定阵列中删除重复项时遇到问题。我写了一个程序,但它只返回全零。我不能使用哈希集 public class Assignment05a { public static void main(String args[]) { int[] sourceArray = {1,4,5,4,1,2,3,5,9,7,12,-5,1,4,-1,-5,12,1}; java.util.Arrays.sort(sourceArray);

目前,我在从给定阵列中删除重复项时遇到问题。我写了一个程序,但它只返回全零。我不能使用哈希集

public class Assignment05a 
{
    public static void main(String args[]) 
    {
         int[] sourceArray = {1,4,5,4,1,2,3,5,9,7,12,-5,1,4,-1,-5,12,1};
         java.util.Arrays.sort(sourceArray);
         eliminateDuplicates(sourceArray); 
    }

    public static int[] eliminateDuplicates(int[] list)
    {   
         int[] noDup = new int[list.length]; 

         for (int c = 0; c < list.length-1; c++)
         {
             if (list[c] != list[c+1])
                {
                    list[c] = noDup[c];
                }
         }

         for(int i = 0; i < noDup.length; i++)
         {
             System.out.println(noDup[i]);
         }
         return noDup;
}   
}
公共类分配05a
{
公共静态void main(字符串参数[])
{
int[]sourceArray={1,4,5,4,1,2,3,5,9,7,12,-5,1,4,-1,-5,12,1};
java.util.Arrays.sort(sourceArray);
消除副本(源阵列);
}
公共静态int[]删除副本(int[]列表)
{   
int[]noDup=newint[list.length];
for(int c=0;c
我想我们可以更新
消除重复的方法,如下所示:

public static int[] eliminateDuplicates(int[] list){   
     List<Integer> noDup = new ArrayList<Integer>(); 
     noDup.add(list[0]);
     for (int c = 1; c < list.length-1; c++){
         if(!noDup.contains(list[c])){
             noDup.add(list[c]); 
         }
     }
     int[] noDupArray = new int[noDup.size()];
     for(int i = 0; i < noDup.size(); i++){
         noDupArray[i] = noDup.get(i);
         System.out.println(noDup.get(i));
     } 
     return noDupArray;
} 
public static int[] eliminateDuplicates(int[] list){   
     int[] noDup = new int[list.length]; 
     noDup[0] = list[0];
     int noDupCount = 1;
     for (int c = 1; c < list.length-1; c++){
         boolean bAlreadyAdded = false;
         for (int d = 0; d < noDup.length-1; d++){
           if (noDup[d] == list[c]){
               bAlreadyAdded = true;
            }
         }
         if(!bAlreadyAdded){
             noDup[noDupCount++] = list[c]; 
         }
     }
     int[] newUniques = new int[noDupCount];
     for(int i = 0; i < noDupCount; i++){
         newUniques[i] = noDup[i];
         System.out.println(noDup[i]);
     }
     return newUniques;
} 

您可能可以将每个字符映射到布尔数组,并根据该字符在布尔数组索引中的ascii值将其设置为true或false。看看这个:

我还没有冒险进入ArrayList,所以我尝试只使用数组编写这个程序。谢谢你的帮助。我用一个没有ArrayList的替代选项更新了答案。非常感谢。我现在明白了。