Java 如何从要排序的文本文件的内容创建数组?
这是一个程序,它获取这三个数组并使用插入排序对它们进行排序,并计算排序时每个数组执行的比较和交换的数量 我现在尝试测试在文本文件上制作的其他三个数组。这三个文本文件只是数字列表,第一个文本文件名为“array4.txt”,其数字列表按顺序包含1到2000个数字 第二个文件名为“array5.txt”,其数字列表按降序包含2000到1。最后,第三个文件名为“array6.txt”,其数字列表包含从1到2000的随机混合数字列表,包括1和2000,没有重复 我的目标是读取这些文件并将它们的值放入实际数组中,然后使用插入排序方法读取它们,对它们进行排序,并计算比较和交换的数量,就像我对前三个数组所做的那样 我对Java非常陌生,不知道如何做到这一点Java 如何从要排序的文本文件的内容创建数组?,java,arrays,text-files,insertion-sort,Java,Arrays,Text Files,Insertion Sort,这是一个程序,它获取这三个数组并使用插入排序对它们进行排序,并计算排序时每个数组执行的比较和交换的数量 我现在尝试测试在文本文件上制作的其他三个数组。这三个文本文件只是数字列表,第一个文本文件名为“array4.txt”,其数字列表按顺序包含1到2000个数字 第二个文件名为“array5.txt”,其数字列表按降序包含2000到1。最后,第三个文件名为“array6.txt”,其数字列表包含从1到2000的随机混合数字列表,包括1和2000,没有重复 我的目标是读取这些文件并将它们的值放入实际
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();
}
import java.util.Scanner;
导入java.io.*;
公共类InsertionSort
{
公共静态void main(字符串args[])引发IOException
{
int[]数组={1,2,3,4,5,6,7,8,9,10};
int[]Array2={10,9,8,7,6,5,4,3,2,1};
int[]数组3={1,10,2,9,3,8,4,7,5,6};
System.out.println(“插入排序:”);
System.out.println();
System.out.println(“最佳情况:”);
打印阵列(数组);
插入排序(数组);
System.out.println(“最坏情况:”);
打印阵列(Array2);
插入排序(Array2);
System.out.println(“平均案例场景:”);
打印阵列(阵列3);
插入排序(数组3);
}
公共静态void insertionSort(int[]列表)
{
综合比较=0,交换=0;
for(int i=1;i0&&list[j]
}给你:
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
}
public void getIt(){
List ints=new ArrayList();//临时持有者
尝试(Scanner Scanner=new Scanner(“filename.txt”){//打开一个扫描我们文件的扫描仪
forEachRemaining(第->{//行)遍历文件中的每一行
String[]numberStrings=line.split(“,”)//如果存在逗号,则逗号是假定的delimeter
对于(int x=0;x
如果每行只有一个数字,则不需要for循环或foreachreserving内的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();
}
}
package.com公司;
导入java.util.Scanner;
导入java.io.File;
导入java.io.FileNotFoundException;
公共班机{
公共静态void main(字符串[]args)引发FileNotFoundException{
//将array.txt替换为txt文件的名称和路径
Scanner fileScanner=new Scanner(新文件(“array.txt”);
//计数器变量,以便我们知道所需数组的大小
int计数器=0;
//遍历文件,计算整数的数量并递增计数器变量
while(fileScanner.hasnetint()){
计数器++;
fileScanner.nextInt();
}
//将扫描仪重置为txt文件的开头
fileScanner=newscanner(新文件(“array.txt”);
//将每个整数扫描到数组中
int[]数组=新的int[计数器];
对于(int i=0;i