如何从要排序的文本文件的内容创建一个数组?

How to create an array from the contents of a text file to be sorted?

这是一个程序,它获取这三个数组并使用插入排序对它们进行排序,并计算排序时对每个数组执行的比较和交换次数。

我现在正在尝试测试在文本文件上创建的其他三个数组。这三个文本文件只是数字列表,第一个文本文件称为 "array4.txt",其数字列表依次包含 1 到 2000。

第二个文件名为 "array5.txt",其数字列表包含 2000 到 1 的降序排列。最后,第三个文件名为 "array6.txt",其数字列表包含从 1 到 2000 的随机混合数字列表,其中 1 和 2000 没有重复。

我的目标是读取这些文件并将它们的值放入一个实际的数组中,然后让我的插入排序方法读取它们、对它们进行排序并计算比较和交换的次数,就像我对前三个文件所做的那样阵列。

我是 Java 的新手,不知道该怎么做。

import java.util.Scanner;
import java.io.*;

public class InsertionSort
{
    public static void main(String args[]) throws IOException
    {    
    int[] Array  = {1,2,3,4,5,6,7,8,9,10};  
    int[] Array2 = {10,9,8,7,6,5,4,3,2,1};
    int[] Array3 = {1,10,2,9,3,8,4,7,5,6};

    System.out.println("Insertion Sort: ");
    System.out.println();
    System.out.println("Best Case Scenario: ");
    printArray(Array);
    insertionSort(Array);

    System.out.println("Worst Case Scenario: ");

    printArray(Array2);

    insertionSort(Array2);

    System.out.println("Average Case Scenario: ");
    printArray(Array3);
    insertionSort(Array3);
}

public static void insertionSort(int[] list) 
{
    int comps = 0, swaps = 0;

    for(int i = 1; i < list .length; i++) {

        int j = i;      

        // compare i with sorted elements and insert it
        // sorted elements: [0..i-1]
        while (j > 0 && list[j] < list[j - 1]) {

            int temp = list[j];
            list[j] = list[j - 1];
            list[j - 1] = temp;

            swaps++;
            comps++;  // loop condition true

            j--;
        }
        comps++; // checking loop condition when false
    }
    //printArray(list);

    System.out.println("Comparisons: " + comps 
        + " Swaps: " + swaps);
    System.out.println();
}

static void printArray(int[] array){

    for(int i=0; i < array.length; i++)
    {  
        System.out.print(array[i] + " ");
    } 
    System.out.println();

}

}

给你:

public void getIt() {
    List<Integer> ints = new ArrayList(); //temporary holder

    try (Scanner scanner = new Scanner("filename.txt")) { //open a scanner that will scan our file
        scanner.forEachRemaining(line -> { //iterate through each line in our file
            String[] numberStrings = line.split(","); // the comma is your presumed delimeter IF one exists
            for (int x = 0; x < numberStrings.length; x++) { // loop through each item separated by a comma on each line
                ints.add(Integer.parseInt(numberStrings[x])); // turn this string into an int and add it to your list
            }
        });
    }
    Integer[] integerArray = ints.toArray(new Integer[ints.size()]); //transform our list into an array
}

如果每行只有一个数字,则不需要 for 循环或 forEachRemaining

内部的 line.split

这是我想到的。希望能帮助到你!

package com.company;

import java.util.Scanner;
import java.io.File;
import java.io.FileNotFoundException;

public class Main {

    public static void main(String[] args) throws FileNotFoundException {
        // Replace array.txt with the name of your txt file and your path
        Scanner fileScanner = new Scanner(new File("array.txt"));
        // Counter variable so we'll know the size of the array we'll need
        int counter = 0;
        // Iterate through the file counting the number of integers and incrementing the counter variable
        while(fileScanner.hasNextInt()){
            counter++;
            fileScanner.nextInt();
        }
        // Reset the scanner to the beginning of the txt file
        fileScanner = new Scanner(new File("array.txt"));

        // Scan each integer into the array
        int [] array = new int[counter];
        for (int i = 0; i < array.length; ++i) array[i] = fileScanner.nextInt();
    }
}