一个作业:从一篇文章中取单词,并按字母顺序进行了排列(已经实现)
          现在根据这组单词,分别列出他们所在的行 ,比如 like {1,6,26}
          不是这一个单词,是这一组已经排序的单词都要找出来,这个如何去实现呢?
   
          我这个方法已经可以实现找一个单词,但是无法按排序的单词组,按从小到大的顺序进行找。我贴了方法,帮我看看吧,裸身跪求了!!!          public String[] lecteur (String file) {
        List<String> liste1 = new Vector<String>();
        List<String> liste2 = new Vector<String>();
        String chbuf;
        
        String table[]=new String[3000];
        //for( motListe1=0;motListe1<liste1.size();motListe1++)
        try {            File f1 = new File(file);
            Scanner sc1 = new Scanner(f1,"ISO-8859-1");
            while (sc1.hasNext()) {
               chbuf = nettoyage(sc1.next()) ;
                if (!chbuf.equals("")){
                    ajouter_elt(chbuf, liste1);
                }
               Collections.sort(liste1);            //par ordre
            }
            for(int i=0;i<liste1.size();i++)        //create la table
                 {table[i]=liste1.get(i);           //retire le mot dans la liste
                 //System.out.println(i+": "+table[i]);
                 }//            *****************************************************************
            File f2 = new File(file);
            Scanner sc2 = new Scanner(f2,"ISO-8859-1");
            
            while (sc2.hasNextLine()) {
               numbreLine++;
               chbuf = nettoyage(sc2.nextLine()) ;
                if (!chbuf.equals("")){
                    ajouter_elt(chbuf, liste2);
                }
                String[] word = chbuf.split(" ");
                Pattern p1=Pattern.compile("[a-zA-Zé\\à\\ê\\è]{4,20}");
                for(int j=0;j<word.length;j++){
                    Matcher matcher1=p1.matcher(word[j]);
                    while(matcher1.find())
                    m.put((String)matcher1.group(),0);
                }                Set<String> set = m.keySet();              
                for(int i=0;i<word.length;i++)                {
                Matcher matcher1=p1.matcher(word[i]);                Iterator<String> it = set.iterator();
                while(matcher1.find()){
                String wordNew=matcher1.group();                while(it.hasNext()){
                String k = nettoyage(it.next());                if(wordNew.equals(k)){                    int c = m.get(k);
                    c++;
                    m.put(nettoyage(wordNew),c);
                }
//******************************************************************************
                 
                 if(liste1.get(l).equals(wordNew)){
                    int c = 0;
                    c++;
                    m2.put(numbreLine,c);
                }
            }
          }    
        }                 m.clear();
            }           for(int line=0;line<liste1.size();line++)
              System.out.println(liste1.get(line)+":"+m2);        } catch (FileNotFoundException ex) {
            Logger.getLogger(Question3_Refaire_4.class.getName()).log(Level.SEVERE, null, ex);
        }
        Collections.sort(liste2);
        return liste2.toArray(new String[0]);
    }
下面还有自己写的方法,这只是部分,谢谢了!!!

解决方案 »

  1.   

                    if(liste1.get(l).equals(wordNew)){ 
                        int c = 0; 
                        c++; 
                        m2.put(numbreLine,c); 
                    } 
    比较就是这一段,liste1是已经排序的单词组,wordNew我是按行找的单词
    用liste1的单词,按行进行和wordNew比较,然后如果有相同就输出行到m2(hashMap)中
    这是这个程序的用意,但我只能找一个单词比如liste1.get(1),我现在要把1变为动态的变量
    变成liste1.size(),可以这样一来,程序永远只能找第一个liste1.get(0),始终无法自增大
    题目应该说清楚了,大家帮我看看,问题出在哪里,裸身跪谢
      

  2.   

    yongMap,KEY=单词,VALUE=List<Integer>这样存就可以了.
      

  3.   

    if(liste1.get(l).equals(wordNew)){
                        int c = 0;
                        c++;
                        m2.put(numbreLine,c);
                    } map.put(wordNew,map.get(wordNew)+" "+numberLine);
      

  4.   

    你说你每次都找到的是liste1.get(0)  那既然这样  你找到这个后再把这个值删了  根据list长度动态的  那么下次使用get(0)其实就是之前的get(1)了
    还有  我也很纳闷  既然可以找到list(0)  怎么可能找不到后面的呢   纳闷....