每当我调用“ InsertionSort”方法时,输出文件就会显示为空?我应该在哪里调用此方法?

时间:2019-04-24 08:00:37

标签: java sorting arraylist filereader insertion-sort

*在此程序中,我正在读取两个文件,然后删除所有标点符号,然后应用“ Insertion算法”(我在该程序中创建了一个方法)。每次我都在调用包含“ InsertionSort”的程序作为一个空输出文件正在生成并且排除此方法程序运行正常。

    import java.util.*;
        import java.io.*;
        public class LexicanTester {
    // Sorting method
            public static void InsertionSort(ArrayList<String> List)
            {
                for(int i=1; i<List.size(); i++ )
                {
                    String value = List.get(i);
                    int n = List.size();    
                    while(true )
                    {
                        if  (i==0)
                        {
                            List.set(0,value);
                        }
                        else if(List.get(i-1).compareTo(value)<=0)
                        {
                            List.set(i,value);
                            i--;
                        }
                        else
                        {
                            List.set(i, List.get(i-1));
                            i--;
                        }
                    }
                }
            }
            public static void main(String args[]) throws IOException
            {
                Scanner sc1 = new Scanner(new File("file name")); 
                Scanner sc2 = new Scanner(new File("file name"));
                ArrayList<String> list = new ArrayList<String>();
                FileWriter writer = new FileWriter ("C:\\Users\\amank\\eclipse-workspace\\DataStructures\\src\\output.txt");; 

            try {
// File read and adding to list
                    while (sc1.hasNext())
                    {
                        list.add(sc1.nextLine().trim().toLowerCase().replaceAll("\\p{P}", "").replaceAll("[0-9]+",""));
                    }

                    while (sc2.hasNext())
                    {
                        list.add(sc2.nextLine().trim().toLowerCase().replaceAll("\\p{P}", "").replaceAll("[0-9]+",""));
                    }
                    String newLine = System.getProperty("line.separator");
                    String newLine2 = System.getProperty("line.separator");


                    InsertionSort(list); // method to sort available text in the files
                    // writting in new file
                    for(String str: list)
                    {
                        writer.write(str + newLine);
                    }
                    for(String str1: list)
                    {
                        writer.write(str1 + newLine2);
                    }
                    sc1.close();
                    sc2.close();
                    writer.close();

                }
                catch (Exception e) {
                    System.out.println("file not found");
                }
            }
        }*

1 个答案:

答案 0 :(得分:0)

因为InsertionSort在while循环中具有“死代码”。如下修改InsertionSort方法。

  public static void InsertionSort(ArrayList<String> List) {
        for (int i = 1; i < List.size(); i++) {
            String value = List.get(i);
            int j = i - 1;
            while (j >= 0 && value.compareTo(List.get(j)) < 0) {
                List.set(j + 1, List.get(j));
                j--;
            }
            List.set(j + 1, value);
        }
    }