簡體   English   中英

從二維數組讀取行

[英]Reading lines from 2 dimensional arrays

我正在嘗試從二維數組中讀取。 該代碼的作用是首先將.txt文件內容存儲到2d數組中,每個元素一行。 然后,它將用戶輸入與每個數組進行比較,以查找相似之處。 任何相似性將存儲在另一個數組中。

這里的事情是比較部分不起作用。 有什么暗示嗎?

謝謝。

import java.awt.Point;
import java.io.File;
import java.util.Arrays;
import java.io.FileNotFoundException;
import java.util.Scanner;

public class Main {
    static int RowCheck =0;
    static int C_Row = 0;
    static int S_Row = 0;


    static String lexicon[][] = new String[3000][10];
    static String results[][] = new String[100][10];


    private static String find2DIndex(Object[][] array, Object search) {

        if (search == null || array == null) return null;

        for (int rowIndex = 0; rowIndex < array.length; rowIndex++ ) {
           Object[] row = array[rowIndex];
           if (row != null) {
              for (int columnIndex = 0; columnIndex < 2; columnIndex++) {
                 if (search.equals(row[columnIndex])) {

                     for(int i=0; i<2; i++)
                          for(int j=0; j<=10; j++)
                            lexicon[i][j]=results[i][j];

                         return Arrays.deepToString(results);
                 }
              }
           }
        }
        return null; // value not found in array
    }


    public static void main(String[] args) throws FileNotFoundException {

        File testlex = new File("C:\\Users\\Harry\\Documents\\testlex.txt");
        File testlex2 = new File("C:\\Users\\Harry\\Documents\\textlex2.txt");


        Scanner cc = new Scanner(testlex2);
        Scanner sc = new Scanner(testlex);

        while (sc.hasNextLine()){
            int column = 0;
            String line = sc.nextLine();
            sc.useDelimiter("/ *");
            if (line.isEmpty())
                continue;

            C_Row = C_Row + 1;
            column = 0;

            String[] tokens = line.split("\\s");

            for (String token : tokens) {
                if (token.isEmpty())
                    continue;
                lexicon[C_Row][column] = token;  
                column++; 
            }
        }

        while (cc.hasNextLine()){
            int column = 0;
            String line = cc.nextLine();
            cc.useDelimiter("/ *");
            if (line.isEmpty())
                continue;

            S_Row = S_Row + 1;
            column = 0;
            String[] tokens = line.split("\\s");
            for (String token : tokens) {
                if (token.isEmpty())
                    continue;

                lexicon[S_Row][column] = token;  
                column++; 
            }
        }

        sc.close();
        cc.close();

        find2DIndex(lexicon, "abash");
        System.out.println(C_Row);

     }    
}

這行代碼會將searchrow[columnIndex]Object類型的Object

if (search.equals(row[columnIndex]))

因此它將比較參考。 您似乎想比較String對象的內容。 您可以通過2種方式為此修改find2DIndex

  1. 將簽名更改為

     private static String find2DIndex(String[][] array, String search) 

    並用String替換函數中出現的Object

  2. 將其轉換為通用方法

     private static <T> String find2DIndex(T[][] array, T search) 

    並用T替換函數中出現的任何Object

在兩種情況下, equals現在都是String的方法。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM