1

新しい Customer オブジェクト (ノードではなく node.data である必要があります) を、顧客オブジェクトの名前のアルファベット順に並べ替えて追加しようとしています。しかし、それは機能していません。リストをソートされていない順序で出力しています(元の順序から変更されていません)。

public void  add(Customer newNode, int dummy){
   if (head == null){ // The first node
      head = tail = this;
      head.setData(newNode);
      size=1;
      return;

   }else{
       CustomerList last = null;
       for(CustomerList node = head; 
               node != null && node.getData().toString().compareTo(newNode.name) < 0; 
                    node = node.next){
          last = node; 

       }
       CustomerList newList = new CustomerList(newNode);
       newList.setNext(last.next);
       last.next = newList;
   }

} // add

txt ファイルからの Customer オブジェクトの入力。もう一度印刷する必要がありますが、アルファベット順 (顧客名) です。

10121,Airgo Fresh ods,OH,870023
10125,Bird Out fittered ,MI,870023
10134,Kit river ,IL,870023
10167,Mouin Gontaods,OR,870021
10178,Theiasu El senter,CA,870022

txt ファイルからデータを読み取り、オブジェクトを作成してリストに追加するコード:

public void byCustomerName()
 {
 records = null;
 System.gc();
 CustomerList.setHead(null);
 records = new CustomerList();
 try
  {
  String line;
  StringTokenizer st;
  String id, name, state, salesrep;
  BufferedReader infile = new BufferedReader(new FileReader("Customer.txt"));
  while ((line = infile.readLine()) != null)
      {
      st = new StringTokenizer(line, ",");
      id = st.nextToken(",");
      name = st.nextToken(",");
      state = st.nextToken(",");
      salesrep = st.nextToken(",");
      records.add(new Customer(id, name, state, salesrep), 99);
      }
  infile.close();
  } catch (IOException x) { System.err.println(x); } 
 } // byCustomerName
4

3 に答える 3

1

あなたのコードにはあらゆる種類の問題があると思います。head1 つには、更新したりtail、最初または最後の要素を置き換えたりするのを見たことがありません。また、 であるlast可能性があるかどうかのチェックもありませんnullCustomerList基礎となるアイテムのいくつかがどのように機能するかを知らずに、これ以上言うのは難しいでしょう.

于 2013-03-15T03:23:45.050 に答える
0

Sudhanshu が既に言ったように、オブジェクトに対してCollections.sort(-)メソッドを使用するか、一意のオブジェクトが必要な場合に備えListて代わりに使用できます。これらはエラーが発生しにくく、信頼性が高いため、TreeSet組み込みのメソッドを利用することをお勧めします。同時に、コードを削減し、コードをマグアップする時間を減らすことができます。java API

于 2013-03-15T05:38:48.010 に答える
0

実は2学期前にまったく同じ課題をやったのですが、決勝が取れなかったのでもう一度受講することにしました。しかし、2 学期の間プログラミングをしなかったので、今は非常に錆びています。今のところあきらめて、最初にクラスを受講したときに思いついた古いソリューションを使用します。

解決:

public void add(Customer newNode, int dummy) {  
    CustomerList before = null;
    boolean inserted = false;
    if (head == null) {  //first node   
        head = tail = this;
        head.setData(newNode);
        return;
    } else {
        CustomerList curr = head;
        while(curr != null) {
            String currentName = curr.getData().getName();
            String newNodeName = newNode.name;
            if (currentName.compareToIgnoreCase(newNodeName) > 0) {

                CustomerList cList = new CustomerList(newNode);
                cList.setNext(curr);//curr is greater than clist, therefore cList's next element is curr
                if(before!=null)
                    before.setNext(cList);
                else {  //this tests the case when 
                    //the newNode goes at the BEGINNING of the list
                    head = cList;
                }
                curr = cList;
                inserted = true;
                return;
            }
            before = curr;
            curr = curr.next;               
        }
    }
    if(!inserted) {
        add(newNode);
    }

} // add
于 2013-03-15T03:43:43.240 に答える