簡體   English   中英

如何從要排序的文本文件的內容創建數組?

[英]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();

}

}

這就是我想出的。 希望能幫助到你!

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();
    }
}

干得好:

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

暫無
暫無

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

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