Java 如何从文本文件中排序并行数组

Java 如何从文本文件中排序并行数组,java,arrays,sorting,Java,Arrays,Sorting,所以我已经尝试了所有的方法,但无法对其进行排序,我必须使用这种排序算法 这是我到目前为止的代码,但当我运行程序时,它会以正常顺序而不是降序打印出数组 BufferedReader filein = new BufferedReader(new FileReader("Employees.txt")); int count = Integer.parseInt(filein.readLine()); String[] names = new String[count];

所以我已经尝试了所有的方法,但无法对其进行排序,我必须使用这种排序算法

这是我到目前为止的代码,但当我运行程序时,它会以正常顺序而不是降序打印出数组

BufferedReader filein = new BufferedReader(new FileReader("Employees.txt"));

    int count = Integer.parseInt(filein.readLine());
    String[] names = new String[count];
    int[] years = new int[count];
    int i;
    int pass;
    int loc;
    int temp;
    String tempNames;
    String passNames;

    System.out.println("My Company Employee Search");
    System.out.println("--------------------------");
    System.out.println("");



    for(i=0; i < count-1; i++) {
        names[i] = filein.readLine();
        years[i] = Integer.parseInt(filein.readLine());
    }//populate the arrays from filein

    for(pass = 0; pass<count-1;pass++){
        loc = pass;
        for(i=pass+1; i<count; i++) {
            if(years[i]<years[loc]) {
                loc = i;
            }//end if
        }//end of inner loop



        temp = years[loc];
        years[loc] = years[pass];
        years[pass] = temp;

        names[loc]=names[pass];

        System.out.println(years[loc]+"  "+names[loc]);
    }//end of containg loop

创建一个普通的旧Java对象(a),并将文件内容存储在该POJO的实例中。只要您实现了POJO,您就可以使用它来订购您的POJO

编辑

然后,您需要更改第二次交换

names[loc]=names[pass];
盲目替换
名称[loc]
,而不保存以前的值

String tempName = names[loc];
names[loc] = names[pass];
names[pass] = tempName;
试试这个代码

public static void main(String[] args) throws IOException {
    // TODO code application logic here
    BufferedReader filein = new BufferedReader(new FileReader("C:\\Employees.txt"));

    int count = Integer.parseInt(filein.readLine());
    String[] names = new String[count];
    int[] years = new int[count];
    int i;
    int pass;
    int loc;
    int temp;
    String tempNames;
    String passNames;

    System.out.println("My Company Employee Search");
    System.out.println("--------------------------");
    System.out.println("");



    for (i = 0; i < count ; i++) {
        names[i] = filein.readLine();
        years[i] = Integer.parseInt(filein.readLine());
        //System.out.println("")
    }//populate the arrays from filein


    for(int j=0;j<years.length;j++)
    {
        for(int k=0;k<j;k++)
        {
            if(years[j]>years[k])
            {
                temp=years[j];
                years[j]=years[k];
                years[k]=temp;

                tempNames=names[j];
                names[j]=names[k];
                names[k]=tempNames;
            }
        }
    }

    for(int j=0;j<years.length;j++)
    {
        System.out.println(" Name = "+names[j]+" And Year = "+years[j]);
    }
}
publicstaticvoidmain(字符串[]args)引发IOException{
//此处的TODO代码应用程序逻辑
BufferedReader filein=新的BufferedReader(新文件阅读器(“C:\\Employees.txt”);
int count=Integer.parseInt(filein.readLine());
字符串[]名称=新字符串[计数];
整数[]年=新整数[计数];
int i;
国际通行证;
int loc;
内部温度;
字符串名称;
字符串密码;
System.out.println(“我的公司员工搜索”);
System.out.println(“-----------------------------------”);
System.out.println(“”);
对于(i=0;i对于(int j=0;j这是因为您在更改名称[loc]和年份[loc]的值后正在打印它们

首先,按照我在评论中的建议,正确交换name[]的内容
第二次打印
name[pass]和year[pass]
因为它们包含较小的值,而不是
loc

我必须使用这种排序算法,我不能使用数组。排序(Object[])为什么不进行名称交换?name[loc每次分配名称[pass]时都会丢失它的值。我不应该这么说。(这表明我很羡慕这个结果),但首先你说你必须使用那个特定的算法,然后你用不同的算法标记出正确答案
public static void main(String[] args) throws IOException {
    // TODO code application logic here
    BufferedReader filein = new BufferedReader(new FileReader("C:\\Employees.txt"));

    int count = Integer.parseInt(filein.readLine());
    String[] names = new String[count];
    int[] years = new int[count];
    int i;
    int pass;
    int loc;
    int temp;
    String tempNames;
    String passNames;

    System.out.println("My Company Employee Search");
    System.out.println("--------------------------");
    System.out.println("");



    for (i = 0; i < count ; i++) {
        names[i] = filein.readLine();
        years[i] = Integer.parseInt(filein.readLine());
        //System.out.println("")
    }//populate the arrays from filein


    for(int j=0;j<years.length;j++)
    {
        for(int k=0;k<j;k++)
        {
            if(years[j]>years[k])
            {
                temp=years[j];
                years[j]=years[k];
                years[k]=temp;

                tempNames=names[j];
                names[j]=names[k];
                names[k]=tempNames;
            }
        }
    }

    for(int j=0;j<years.length;j++)
    {
        System.out.println(" Name = "+names[j]+" And Year = "+years[j]);
    }
}