【问题标题】:How can I check if the elements of an ArrayList equal another value in Java?如何检查 ArrayList 的元素是否等于 Java 中的另一个值?
【发布时间】:2016-12-03 07:05:02
【问题描述】:

特别是如果说ArrayList 是由字符串元素组成的,我想检查那些Strings 中的每个字母。这是我尝试过的,但显然它不起作用:

public void fillBoard()
    {
        board = new char[ROW][COL];

        for (String s: words){

            for(int rows = 0; rows < board.length; rows++)
            {
                for(int cols = 0; cols < board[rows].length; cols++)
                {
                    if(!s.equals(board[rows][cols])) 
                    {                          
                        board[rows][cols] = randomChar();
                    }
                }   

            }
        }
    }

我试图将2D 数组中的每个字符与构成ArrayListStrings 中的字母进行比较。

ArrayListArrayList&lt;String&gt; words = new ArrayList&lt;String&gt;();,它由用户在提示时输入单词时填充。所以它看起来像 {"hello","goodbye","words",...}

board 是一个二维字符数组,维度由用户输入定义

然后通过这种方法由随机字符填充:

public char randomChar()
    {
        char alphabet[] = {'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k',
                'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x',
                'y', 'z'};

        return alphabet[(char)(alphabet.length * Math.random())];
    }

}

其目的是char board[][] 数组也可能包含随机放置的单词,并且当板更新时,words ArrayList 中不属于 string 的所有字母再次随机化,保持用户输入的单词以及放置在数组中的位置,有点像战舰程序中每回合后更新棋盘。

【问题讨论】:

  • 将你的字符串转换成一个字符数组,然后尝试比较,顺便说一下,你将完整的字符串与二维数组的每个字符进行比较,它永远不会使用这个逻辑,将一行与字符串进行比较
  • 你重新初始化你的 char 数组,所以它永远不会工作。提供有关输入数据的更多信息,也许提供一个示例以便我们提供帮助。
  • @Default71721 我添加了一些额外的信息

标签: java arraylist multidimensional-array


【解决方案1】:

您是直接将字符串与需要更改的字符进行比较,请查看以下步骤:

(1) 迭代char数组的每一行

(2) 从list 获取string

(3) 现在遍历数组的列并与string中的每个字符进行比较

(4) 在每一步,我都添加了验证来检查它们的长度是否可比break,如果不是,您实际上可以根据您的要求进行修改。

您可以使用内联 cmets 参考以下代码:

public static void fillBoard() {
        int x=2;//change row size accordingly
        int y=2;//change row size accordingly
        char[][] board = new char[x][y];
        //load the board char array

        List<String> words = new ArrayList<>();
        //load the list

        //Iterate each row of the char array
        for(int rows = 0; rows < board.length; rows++) {
            //Check if list size is correct to compare
            if(rows >= words.size()) {
                System.out.println("list size is lower than 
                   the array size,can't caompare these rows");
                break;
            }
            //Get the string from list
            String s = words.get(rows);

            //Iterate across the columns and compare with string character
            for(int cols = 0; cols < board[rows].length; cols++) {
                //check if string length is comparable
                if(s.length() != board[rows].length) {
                    System.out.println("list string length is lower
                         than the array string length,can't caompare ");
                    break;
                }
                if(s.charAt(cols) != board[rows][cols]) {                          
                    System.out.println(s.charAt(cols)+" is different !!!");
                    board[rows][cols] = randomChar();
                }
            }   
        }
    }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2023-04-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-01-03
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多