为什么这个冒泡不能将这个列表在java中按字母顺序输出?

时间:2013-12-06 02:52:45

标签: java sorting

我应该在这个冒泡排序中使用哪些变量?我尝试了多次在线查找教程,但是,他们都主要展示了如何使用较小的数字列表而不是扫描的字符串文件来完成此操作。如果它们具有相同的源和目的地机场代码并且如果它们不相等,我只需要将两条航线视为相等,则应该按字母顺序基于源机场代码进行比较。我真的需要帮助这个伤害我的大脑,我知道这个解决方案可能非常简单。我继续并强调了需要帮助的泡泡分类代码。

public class FlightRouteDriver 
    {

        public static void main(String[] args)
        {
            String routeFile = "FlightRouteData-US.txt";

            @SuppressWarnings("rawtypes")
            ArrayList<Comparable> flRouteList = new ArrayList<Comparable>();

            BufferedReader br = null;
            String line = "";
            try {
                br = new BufferedReader(new FileReader(routeFile));
            } catch (FileNotFoundException e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
            }
            try {
                while ((line = br.readLine()) != null) {
                    String[] dataSet = line.split(";");
                    FlightRoute flRoute = createFlightRoute(dataSet);
                    flRouteList.add(flRoute);
                }
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            try {
                br.close();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

            System.out.println("Uploaded data file contains " + flRouteList.size() + " flight routes.");

            //sorting list of flight route
            System.out.println("Bubble Sort Algorithm running ..." );
            BubbleSort(flRouteList);
            System.out.println("Flight route data sorted..." );

            //Getting user input on source and destination airport codes
            Scanner userInput = new Scanner(System.in);
            System.out.println("Enter Departure Airport code: ");
            String departureAirport = userInput.next();
            System.out.println("Enter Destination Airport code: ");
            String destinationAirport = userInput.next();
            userInput.close();

            //creating a target flight for search
            String[] target = new String[13];

            target[0]="";
            target[1]="0";
            target[2]=departureAirport;
            target[3]="";
            target[4]="01:00";
            target[5]="0";
            target[6]=destinationAirport;
            target[7]="";
            target[8]="01:00";
            target[9]="0";
            target[10]="N";
            target[11]="0";
            target[12]="";

            FlightRoute targetRoute = createFlightRoute(target);

            ArrayList<Comparable> found =  linearSearch (flRouteList, targetRoute);

            System.out.println("\n***Flight Route search result***");

            if(found == null)
                System.out.println("Flight route not found.");
            else
            {
                for (int i = 0; i<found.size(); i++)
                    System.out.println("Flight Routes found. Details are below.\n" + found.get(i));
            }

        }//end of main method



        //  -------------------------------------------------------------------
        //  Creates a FlightRoute object using specified String array object.
        //  -------------------------------------------------------------------
        private static FlightRoute createFlightRoute(String[] dataSet)
        {
            String airline = dataSet[0];
            int airlineID = Integer.parseInt(dataSet[1]);
            String sourceAirportCode = dataSet[2];
            String sourceAirportCity = dataSet[3];
            Time sourceCityDepartureTime = Time.valueOf(dataSet[4]+":00");
            int sourceAirportID = Integer.parseInt(dataSet[5]);
            String destinationAirportCode = dataSet[6];
            String destinationAirportCity = dataSet[7];
            Time destinationCityArrivalTime = Time.valueOf(dataSet[8]+":00");
            int destinationAirportID = Integer.parseInt(dataSet[9]);
            char codeshare = dataSet[10].charAt(0);
            int stops = Integer.parseInt(dataSet[11]);
            String equipment = dataSet[12];

            FlightRoute flRoute = new FlightRoute(airline,airlineID,sourceAirportCode,
                    sourceAirportCity,sourceCityDepartureTime, sourceAirportID,
                    destinationAirportCode,destinationAirportCity, destinationCityArrivalTime,
                    destinationAirportID,codeshare, stops, equipment );

            return flRoute;
        }//end of createFlightRoute method


        //  -------------------------------------------------------------------
        //  Sorts the specified list of objects using a bubble sort algorithm.
        //  -------------------------------------------------------------------




         static void BubbleSort(ArrayList<Comparable> flRouteList) {
            String t;
            for(int i=0; i<flRouteList.length; i++) {
                for(int j=0; j<flRouteList.length-1-i; j++) {
                if(Comparable[j].compareTo(dataSet[j+1])>0) {
                    t= array[j];
                    array[j] = array[j+1];
                    array[j+1] = t;
                    }
                }
              }
            }

0 个答案:

没有答案