【问题标题】:Whenever I am calling "InsertionSort" method the output file is appearing empty? Where should I call this method?每当我调用“InsertionSort”方法时,输出文件显示为空?我应该在哪里调用这个方法?
【发布时间】:2019-04-24 08:00:36
【问题描述】:

*在这个程序中,我正在读取两个文件,而不是删除所有标点符号,而不是应用“插入算法”(我在这个程序中创建了一个方法)。每次我调用这个程序,包括“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");
                }
            }
        }*

【问题讨论】:

    标签: java sorting arraylist filereader insertion-sort


    【解决方案1】:

    因为 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);
            }
        }
    

    【讨论】:

    • 实施后此文本出现在文件中但未排序。
    • 是未排序还是升序排序?
    猜你喜欢
    • 2016-08-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-08-19
    • 1970-01-01
    • 1970-01-01
    • 2013-05-10
    相关资源
    最近更新 更多