有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java避免没有集合的重复?

我是一名Java初学者,还没有机会学习如何在数组中存储值时避免重复值

String[] newAlphabet = new String[26];

for(int I = 0; I < newAlphabet.length; I++){

 int random = (65 + (int)(Math.random() * ((90 - 65) + 1));
 char ascii = (char)random;
 String letters = ascii + "";

if(letters != newAlphabet[0] && letters != newAlphabet[1] ... so on and so on until
                                                               newAlphabet[25])
     newAlphabet[I] = letters; 
}//end 

这是我的部分程序的伪代码,它的目的是避免数组中有重复的字母

我遇到的问题是在if语句中。除了将letters != newAlphabet[]键入25之外,还有其他方法吗

我在stackedoverflow中看到了一些我应该使用的论坛HashSet,但我还没有学会?如果允许的话,我可以问我的老师,但是有没有其他方法可以避免这个问题呢

我一直在考虑使用for each循环来搜索数组中的所有元素,但我没有考虑足够长的时间来确定该计划是否有效


共 (5) 个答案

  1. # 1 楼答案

    您可以使用asList方法:

    if( Arrays.asList(newAlphabet).contains(letters) ) {
        newAlphabet[I] = letters; 
    }
    

    这并不是最有效的,但是由于你的数组只有26个元素,我更倾向于清晰而不是效率

    一些解释:asListArrays类上的一个静态方法。这只是意味着我们不必创建Arrays对象来调用它。我们只是简单地说Arrays.asList()并将参数传递给它。asList方法将数组(newAlhpabet在本例中)作为参数,并从中构建一个java.util.List。这意味着我们可以对返回值调用List方法contains()List上的一个方法,如果List包含与参数相等的元素(在本例中为letters),则返回true

  2. # 2 楼答案

    在您谈论Java初学者课程时,我假设您对编程相当陌生。因此,与其只给你一个库函数来为你做这件事,不如让我们简单介绍一下如何使用基本代码来实现这一点,这样你就可以更好地了解幕后的情况

    首先,对于任何重复的动作,思考循环。对于新字母表中的每个字母,您都要检查要添加的字母是否与之匹配。所以

    boolean exists = false; //indicates whether we have found a match
    for (int j = 0; j < 26; j++) { //for each letter in the new alphabet
        //true if this one, or a previous one is a match
        exists = exists || letters == newAlphabet[i]; 
    }
    //if we don't have a match, add the new letter
    if (!exists) newAlphabet[I] = letters;
    

    现在,当您在构建新的字母表时,对于大多数运行此代码的情况,我们没有完整的26个字母,所以只检查我们定义的新字母表的部分:

    boolean exists = false; 
    for (int j = 0; j < I; j++) { //note in this line we stop before the insertion point
        exists = exists || letters == newAlphabet[i]; 
    }
    if (!exists) newAlphabet[I] = letters;
    

    最后,我们不需要一直检查是否已经找到匹配项,因此我们可以在找到匹配项时将循环更改为停止:

    boolean exists = false;
    int j = 0;
    while (!exists && j < I) { //we now also stop if we have already found a match
        exists = letters == newAlphabet[i]; 
        //as we are stopping at the first match, 
        //we no longer need to allow for previous matches
    }
    if (!exists) newAlphabet[I] = letters;
    
  3. # 3 楼答案

    你有几个选择

    1. 在数组中循环,基本上完成你现在正在做的事情

    2. 按排序顺序插入字符,以便执行二进制搜索以确定列表中是否已有字母。作为奖励,如果使用选项2,您将已经知道插入点

    查看阵列。binarySearch():http://docs.oracle.com/javase/7/docs/api/java/util/Arrays.html

  4. # 4 楼答案

    你可以用这个:

    if(Arrays.binarySearch(newAlphabet, letters) < 0){
      newAlphabet[I] = letters;
     }
    

    您应该包括一个while循环,以确保在移动到下一个之前填充数组的每个索引,或者可以使用Arrays.binarySearch的返回值(-(insertion index) - 1)来填充数组,并在数组被填充时退出

  5. # 5 楼答案

    根据这一行,看起来你要做的就是按照其他顺序生成字母AZ

    int random = (65 + (int)(Math.random() * ((90 - 65) + 1));
    

    如果我理解正确,那么你真正想要做的就是把字母表洗牌:

    // Initialize new alphabet array
    String originalAlphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
    char[] newAlphabet = originalAlphabet.toCharArray();
    // Shuffle the new alphabet by swapping each character to a random position
    for (int i=0; i<26; i++) {
      int j = (int)(Math.random() * 26);
      char temp = newAlphabet[i];
      newAlphabet[i] = newAlphabet[j];
      newAlphabet[j] = temp;
    }
    // Print the new alphabet
    for (int i=0; i<26; i++) {
      System.out.print(newAlphabet[i]);
    }
    System.out.println();
    

    下面是一个示例输出:VYMTBIPWHKZNGUCDLRAQFSOEJX