如果已经给出了2D数组,如何制作查找表?

时间:2013-10-24 18:40:26

标签: java arrays matrix

我有一个2D数组,我将其命名为查找表,如下所示:

123   256   19   52369564875214
147   307   51   12654987625425
136   94    14   65913210210871
301   818   23   74120032154894
215   103   81   54680045569748
...

......等等(数组大小为700x4)

这项工作的目的是要求用户输入三个数字,比如a,b和c。 然后,此代码应查看表(上面的数组)并返回相应的值d,其中d是从最后一列中提取的。例如,如果用户输入(147,307,51),那么代码应该给用户12654987625425.事实上,作为函数f(a,b,c)= f(147,307,51)= 12654987625425。

package arrayread;

import java.io.FileReader;
import java.util.ArrayList;
import java.util.Scanner;

public class ArrayRead {
    //  
    public static void main(String [] args)
    {
        ArrayRead ar = new ArrayRead();
        ar.readArray("C:/Users/Desktop/tsp2.txt",",");
        String arr[][] = ar.getArray();
        int rows = ar.getRows();
        int cols = ar.getCols();

        for (int r = 0; r < rows; r++) {
            for (int c = 0; c < cols; c++) {
                System.out.print(arr[r][c]+" ");
            }
            System.out.println();
        }   
    }

    private String arr[][];
    private int rows, cols;

    String[][] getArray()
    {
        return arr;
    }

    int getRows()
    {
        return rows;
    }

    int getCols()
    {
        return cols;
    }

    // Read using DELIMITER TAB
    void readArray(String arrayName)
    {
        rows = 0;
        cols = 0;
        int r, c, numElements;
        String ipString, splitString[];
        ArrayList <String>tmpArr = new ArrayList<>();

        try
        {
            Scanner in = new Scanner(new FileReader(arrayName));
            while(in.hasNext())
            {
                ipString = in.nextLine();
                splitString = ipString.split("\t");
                if (rows==0)
                {
                    cols = splitString.length;
                }
                for (c=0;c<cols;c++)
                {
                    tmpArr.add(splitString[c]);
                }
                rows++;
            }
            in.close();

            arr = new String[rows][cols];
            numElements = 0;
            for (r=0; r<rows; r++)
            {
                for (c=0; c<cols; c++)
                {
                    arr[r][c] = tmpArr.get(numElements);
                    numElements++;
                }
            }
        }
        catch (Exception e)
        {
            System.out.println("Error occurred!");
        }
    }

    // Read using DELIMITER regex
    void readArray(String arrayName, String regex)
    {
        rows = 0;
        cols = 0;
        int r, c, numElements;
        String ipString, splitString[];
        ArrayList <String>tmpArr = new ArrayList<>();

        try
        {
            Scanner in = new Scanner(new FileReader(arrayName));
            while(in.hasNext())
            {
                ipString = in.nextLine();
                splitString = ipString.split(regex);
                if (rows==0)
                {
                    cols = splitString.length;
                }
                for (c=0;c<cols;c++)
                {
                    tmpArr.add(splitString[c]);
                }
                rows++;
            }
            in.close();

            arr = new String[rows][cols];
            numElements = 0;
            for (r=0; r<rows; r++)
            {
                for (c=0; c<cols; c++)
                {
                    arr[r][c] = tmpArr.get(numElements);
                    numElements++;
                }
            }
        }
        catch (Exception e)
        {
            System.out.println("Error occurred!");
        }
    }
}

1 个答案:

答案 0 :(得分:0)

所以基本上,如果我理解正确,你给了arr [x] [0,1,2]并且你想要返回arr [x] [3]

如果是这种情况,你所做的是:

for(int i = 0; i < 700; i++){
    if(arr[i][0] == entry1 && arr[i][1] == entry2 && arr[i][2] == entry3)
        return arr[i][3];
}

编辑: 要从命令行读取,您需要执行以下操作:

//  open up standard input
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
try{
    int entry1 = Integer.parseInt(br.readLine());
    int entry2 = Integer.parseInt(br.readLine());
    int entry3 = Integer.parseInt(br.readLine());
}catch(IOException ioe){
    // handle exception
}

PS:对不起延迟