第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問(wèn)題,去搜搜看,總會(huì)有你想問(wèn)的

從文件導(dǎo)入時(shí),TreeSet 僅復(fù)制第一項(xiàng)

從文件導(dǎo)入時(shí),TreeSet 僅復(fù)制第一項(xiàng)

烙印99 2023-06-08 19:31:06
我實(shí)現(xiàn)了一個(gè)名為 EventSet 的類,其中包含一個(gè)帶有自定義比較器的 TreeSet。comparator 應(yīng)該與 equals 一致,因?yàn)樵趯⒃靥砑拥郊现?,TreeSet 似乎使用 compare 或 compareTo 進(jìn)行所有需要的比較。我的應(yīng)用程序需要讀取包含一系列命令的文本文件,一個(gè)可能的命令是導(dǎo)入指定事件的文本文件。因此,一個(gè)假設(shè)的 event.txt 文件包含幾行,例如“IN LOGIN 18082019 ab001 45.457, 9,181 A”,應(yīng)用程序調(diào)用一個(gè)方法來(lái)解析字符串并將其轉(zhuǎn)換為一個(gè) Event 對(duì)象,該對(duì)象被添加到 EventSet 實(shí)例中。這里的問(wèn)題很奇怪:一切正常,除非在命令文件中我嘗試導(dǎo)入相同的事件。txt 文件兩次,文件的第一行被轉(zhuǎn)換為一個(gè)事件,并作為重復(fù)項(xiàng)插入到集合中,即使 equals 和 compare 說(shuō)它是重復(fù)項(xiàng)。無(wú)論我如何更改它,這只會(huì)發(fā)生在文件的第一行。到目前為止,這是我的一些代碼:類事件集:private static EventSet instance;private TreeSet<Event> eventTree;//costruttoreprivate EventSet() {    EventComparator comp = new EventComparator();    this.eventTree = new TreeSet<Event>(comp);}public static EventSet getInstance() {    if (instance == null) {        instance = new EventSet();    }    return instance;}public TreeSet<Event> getEventTree() {    return eventTree;}public void setEventTree(TreeSet<Event> eventTree) {    this.eventTree = eventTree;}public boolean add(Event e) {    return this.eventTree.add(e);}public boolean add(Set<Event> set) {    return this.eventTree.addAll(set);}類事件比較器:public EventComparator() {    super();}@Overridepublic int compare(Event e1, Event e2) {    if(e1.equals(e2)) {        return 0;    } else if(e1.getTimestamp().compareTo(e2.getTimestamp())>=0) {        return 1;    } else {        return -1;    }}
查看完整描述

1 回答

?
紅糖糍粑

TA貢獻(xiàn)1815條經(jīng)驗(yàn) 獲得超6個(gè)贊

已解決:顯然,問(wèn)題是由調(diào)用一個(gè)方法引起的,該方法讀取文件并在另一個(gè)從文件讀取的方法中對(duì)樹執(zhí)行 add() 。我編輯了 import_events() 和 import_cmd() 方法,現(xiàn)在一切正常。


private HashSet<Event> import_events(String path) throws FileNotFoundException, IOException, ParseException {     


    FileReader fr1;

    BufferedReader br1; 

    String current; 

    String[] tokens;

    HashSet<Event> set = new HashSet<Event>();


    fr1 = new FileReader(path);

    br1 = new BufferedReader(fr1); 

    current = br1.readLine(); 


    if (current == null) {

        if (this.controller != null) {

            controller.get_message("Warning: event file is empty, no events to import");

        }

        br1.close(); 

        fr1.close(); 

        return set; 

        }


    InputLine il;

    while (current != null) { 


        current = current.trim(); 


        if (this.controller != null) {

            il = new InputLine(current, controller);

        } else {

            il = new InputLine(current);

        }


        if (il.line_ok()) { 


            tokens = current.split(Pattern.quote(" "));


            RegState reg_state = RegState.valueOf(tokens[0]); 


            String user_id = tokens[3]; 


            LogState log_state = LogState.valueOf(tokens[1]);




            InputLine.d_format.setLenient(false); 

            Date timestamp = InputLine.d_format.parse(tokens[2]);



              String[] latlong = tokens[4].split(","); 

              double lat = Double.parseDouble(latlong[0]); 

              double longi = Double.parseDouble(latlong[1]); 

              Position pos = Position.create(lat, longi);


              if (pos == null) { 

                  if (this.controller != null) {

                      controller.get_message("Error: invalid coordinates at "+current+", event ignored");

                  }

                  break; 

              }


              Emotion emotion = Emotion.valueOf(tokens[5]);


              Event event = new Event(reg_state,log_state, timestamp, user_id, pos, emotion);


              set.add(event);

            }

            current = br1.readLine();

        }

        br1.close(); 

        fr1.close(); 

        return set; 

        }

現(xiàn)在 import_events() 返回文件中包含的事件的 HashSet。


import_cmd() 使用 HashSet 作為在樹上調(diào)用 addAll() 的參數(shù)。


void import_cmd() throws NumberFormatException, ParseException {

    FileReader fr;

    BufferedReader br;

    String current;


    try {

        fr = new FileReader(cmd_path);

        br = new BufferedReader(fr);

    } catch (FileNotFoundException e) {

        if (this.controller != null) {

            controller.get_message("Error: file not found at this location "+cmd_path.getAbsolutePath());

        }

        return;

    }


    InputLine line;

    Set<Event> toAdd=new HashSet<Event>();


    try {

        current = br.readLine();

        while (current != null) {

            if (this.controller != null) {

                line = new InputLine(current, controller);

            } else {

                line = new InputLine(current);

            }

            if (line.cmd_check() == 1) {

                String extracted = line.getIn_line().substring(line.getIn_line().indexOf("(")+1, line.getIn_line().indexOf(")"));

                String path = this.event_path.getAbsolutePath()+File.separator+extracted;


                try {

                    toAdd=import_events(path);

                    if (this.controller != null) {

                        controller.get_message("File "+ extracted + " successfully imported ");

                    }

                } catch (FileNotFoundException e) {

                    if (this.controller != null) {

                        controller.get_message("Error: file not found at "+path);

                    }

                } catch (IOException ioe) {

                    if (this.controller != null) {

                        controller.get_message("Error: unable to read from file at "+path);

                    }

                }

                boolean addOk=EventSet.getInstance().add(toAdd);

                if (this.controller!=null) {

                    if (addOk) {

                        controller.get_message("Events added");

                    } else {

                        controller.get_message("No events to add");

                    }

                }

            } else if (line.cmd_check() == 2) {

                boolean valid = line.validate_date_iterval(line.getIn_line());

                if (valid) {

                    Date[] dates = line.convertIntervaltoDates(line.intervalExtract(line.getIn_line()));

                    createmap(dates[0], dates[1]);

                    if (this.controller != null) {

                        controller.get_message("Map correctly created for "+ line.getIn_line());

                    }

                } else if (this.controller != null) {

                    controller.get_message("Invalid date at "+ line.getIn_line()+": unable to create map");

                }


            }

            current = br.readLine();

        }

        br.close();

        fr.close();

    } catch (IOException e) {

        // TODO Auto-generated catch block

        e.printStackTrace();

    }



}




查看完整回答
反對(duì) 回復(fù) 2023-06-08
  • 1 回答
  • 0 關(guān)注
  • 119 瀏覽
慕課專欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)