【问题标题】:Reading Integers into IntegerType array将整数读入 IntegerType 数组
【发布时间】:2014-02-02 01:42:30
【问题描述】:

我应该编写一个程序,从 IntegerType 数组中的 txt 文件中读取整数。我创建了一个 IntegerType 类,它实现了我的 AnyType 接口,正如您将在代码中看到的那样。然后我应该对数组进行排序并研究在排序期间进行了多少比较和交换以研究效率,我知道是O(N^2)。我在我的代码中设置了断点,这表明整数正在被读入我的 String[] 数字数组。当我尝试将它们添加到我的 IntegerType 数组时,它会命中 while (scan.hasNext()) 代码行并完全跳过我的 for 循环以将整数添加到我的数组中。有人对如何解决这个问题有任何建议吗?谢谢你的时间。这是我的代码:

我的Sorting 班级:

public class Sorting {

    public static void main(String[] args) throws IOException {
        int type, sort;
        Scanner read = new Scanner(System.in);

        //Ask user for data type of input
        System.out.println("Make selection by typing corresponding integer value and pressing Enter.");
        System.out.println("Select type of input:");
        System.out.println("1 = Integer  2 = String");
        type = read.nextInt();

        //Ask user for sorting algorithm desired
        System.out.println("Select sorting algorithm to be used:");
        System.out.println("1 = Insertion  2 = Selection  3 = Bubble");
        sort = read.nextInt();

        //Read in integer values from generated .txt files into corresponding integer arrays
        Scanner scan = new Scanner(new File("descending.txt"));
        String line = scan.nextLine();
        String[] numbers = line.split(" ");
        IntegerType[] worstCase = new IntegerType[numbers.length];

        while (scan.hasNext()) {
            for (int i = 0; i < worstCase.length; i++) {
                worstCase[i] = new IntegerType(scan.nextInt());
            }
        }

        scan = new Scanner(new File("random.txt"));
        line = scan.nextLine();
        numbers = line.split(" ");
        IntegerType[] avgCase = new IntegerType[numbers.length];

        while (scan.hasNext()) {
            for (int i = 0; i < numbers.length; i++) {
                avgCase[i] = new IntegerType(scan.nextInt());
            }
        }

        scan = new Scanner(new File("ascending.txt"));
        line = scan.nextLine();
        numbers = line.split(" ");
        IntegerType[] bestCase = new IntegerType[numbers.length];

        while (scan.hasNext()) {
            for (int i = 0; i < numbers.length; i++) {
                bestCase[i] = new IntegerType(scan.nextInt());
            }
        }

        if ((type == 1 || type == 2) && (sort == 1)) //Insertion Ascending
        {
            System.out.println("Insertion Sort / Ascending / Worst Case");
            Sort.insertionSort(worstCase, worstCase.length);
            System.out.println("Insertion Sort / Ascending / Average Case");
            Sort.insertionSort(avgCase, avgCase.length);
            System.out.println("Insertion Sort / Ascending / Best Case");
            Sort.insertionSort(bestCase, bestCase.length);
        }
    }
}

我的Sort 班级:

public class Sort {

    public static void insertionSort(AnyType[] list, int size) {
        int compare = 0, swap = 0;
        AnyType key;

        for (int i = 1; i < size; i++) {
            key = list[i];
            int j = i - 1;
            compare++;

            if ((j > -1) && (list[j].isBetterThan(key))) {
                list[j + 1] = list[j];
                j--;
                swap++;
            }
            list[j + 1] = key;
        }

        System.out.println("There were " + compare + " comparisons made.");
        System.out.println("There were " + swap + " swaps made.");
    }
}

我的AnyType界面

public interface AnyType { 
    public boolean isBetterThan(AnyType datum);
}

我的IntegerType 班级

public class IntegerType implements AnyType {

    private int number;

    IntegerType() {
        number = 0;
    }

    IntegerType(int i) {
        number = i;
    }

    IntegerType(String s) {
        number = Integer.parseInt(s);
    }

    public boolean isBetterThan(AnyType datum) {
        return (this.number > ((IntegerType) datum).number);
    }

    public int toInteger() {
        return number;
    }
}

【问题讨论】:

  • 如果您的问题是文件读取循环,您可以通过删除大部分代码并专注于这些代码来极大地简化您的问题。它甚至不必涉及您的 IntegerType,因为普通的 Integer 将与此目的相同。我个人对 forwhile 中的嵌套持怀疑态度——看起来后者应该是 if 或者应该合并到 for 的控件中。
  • @keshlam 我明白你的意思,但是当我尝试它时它仍然给了我一个空数组。我意识到我可以这样做; for(int i = 0; i

标签: java arrays sorting integer


【解决方案1】:

我猜该文件在一行中由空格分隔的整数值组成。 无论如何,您应该使用 Scanner.hasNextInt() 方法进行检查。 所以问题就在这里(见 cmets):

    //Read in integer values from generated .txt files into corresponding integer arrays
    Scanner scan = new Scanner(new File("descending.txt"));
    String line = scan.nextLine();     //reads already the whole line
    String[] numbers = line.split(" ");   //now a string array with all the values

    //there is nothing left, everything is already stored in "line"/"numbers" variables
    while (scan.hasNext()) {   
        //something
    }

由于您现在不预先知道会有多少个整数值,因此您需要一个动态数据结构来存储您的 IntegerType 值(例如 ArrayList)。或者你遍历数字数组并将每个字符串值转换为一个 int:

    Scanner scan = new Scanner(new File("descending.txt"));
    String line = scan.nextLine();
    String[] numbers = line.split("\\s+");
    IntegerType[] worstCase = new IntegerType[numbers.length];

    for(int i = 0; i < numbers.length; ++i)
        worstCase[i] = new IntegerType(numbers[i]);  //will work because IntegerType has a constructor which accepts a string 
    }

对您的排序方法的另一条评论:它们不需要数组的长度作为参数,您可以使用 array.length (但这只是我猜的味道)。

【讨论】:

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