java - 如何按特定顺序打印二维数组的某些部分?

标签 java arrays if-statement for-loop printing

我的函数需要接受两个参数,分别称为 data、player。参数数据是一个二维字符串数组,其中包含 nba.txt 的数据。电子表格。参数player是一个字符串。 该函数查找数据中第 0 列(小写版本)包含作为子字符串存储在player(也是小写版本)中的字符串的所有行。对于所有此类行,它会打印相应玩家的统计数据。

我的代码:

 public static void print_player_info(String[][] data, String player)
  {
   String name = player.toLowerCase();
   for(int i = 0; i<data[0].length; i++)
   {
    String namecolumn = data[i][0];
    String rownames = data[0][i];
    for(int j = 0; j<data[0].length; j++)
    {
        String temp = Arrays.toString(data[j]);
        if(temp.toLowerCase().contains(player))
        {
            System.out.println(rownames+": "+data[j][i]);
        }
    }
   }  
  }

如果我的输入一次只检索一个人,它就会以所需的方式工作。

示例:输入:“lebro” 输出:

player: LeBron James
games played: 69
minutes per game: 36.1
etc...

如果我的输入碰巧检索到多个人,它会打印 it stacked:input "jam"

player: James Harden
player: LeBron James
games played: 81
games played: 69
etc...

当我输入“jor”时,它应该找到多个包含“jor”的名称,但它什么也不输出。我想知道如何解决这个问题以及之前的输入:

player: James Harden
games played: 81
etc...
player: LeBron James
games played: 69
etc...

最佳答案

public static void print_player_info(String[][] data, String player){
    // lower case the search string
    String searchString = player.toLowerCase();
    // check every row in the data table except the first
    // because it has the column names in it.
    for(int row = 1; row < data.length; row++){
        // get the player name in this row
        String playerInThisRow = data[row][0];
        // lower case it and check if it contains the search string
        if(playerInThisRow.toLowerCase().contains(searchString)){
            // go throu every column in this row
            for(int column = 0; column < data[0].length; column++){
                // and get the column name and the content
                String columnContent = data[row][column];
                String columnName = data[0][column];        
                // and print it
                System.out.println(columnName +": " + columnContent);
            }  
        }
    }
 }

关于java - 如何按特定顺序打印二维数组的某些部分?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33916647/

相关文章:

java - 为什么 JDBC 将端口 0 视为与空(默认)端口相同?

javascript - 如何组合多个数组中的值以创建新的组合数组

Java数组/逆新手

java - Java中的多个OR条件

java - 带有加号字符的字符串匹配方法返回 false

python - "if"基于命令的语句?

java - 使用 Jackson 2 Java 到 JSON 反序列化器的自定义注释

java - 如果调用 super.method,如何重写第三方库类中的 method() ?

java - Mockito + Spring + @PostConstruct,mock初始化错误,为什么调用@PostConstruct?

arrays - Swift - 将对象数组保存到核心数据,检索时只取回一个对象