简体   繁体   English

使用对象数组创建索引

[英]Creating Index with Array of Objects

I want to create an index using Array Of Objects.The index ,of course, represents the word and the lines that contain that word.I created a class called Pair, that represents the pair (word,lines) ,where the lines are contained in a array of Integers. 我想使用对象数组创建索引。索引当然代表单词和包含该单词的行。我创建了一个名为Pair的类,它表示对(word,lines),其中包含行在整数数组中。 The class is 这个班是

public class Pair
{ private String word;
  private int [] a;
  private int inputSize;

  public Pair(String word,int line)
  {this.word=word;
   a=new int [10];

   a[inputSize++]=line;
  }

  public String getWord()
  {return word;}

  public void addPosition(int line)    //resize
  { if(inputSize==a.length-1)
      {   int[] newA=new int [2*inputSize];
            for(int i=0;i<inputSize;i++)
               { newA[i]=a[i];}

               a=newA;
      }
    a[inputSize++]=line;
    }

  public String getPositions()
  {String s="";
   for(int i=0;i<inputSize;i++)
     {s=s+" "+a[inputSize] ;}
   return s;
  }

  public String toString()
  { String ss="";
     ss=getWord()+" [ " +getPositions()+" ] ";
    return ss;
  }

}

After i created the class Index 创建班级索引之后

 public class Index
{ private Pair [] a;
  private int inputSize;

  public Index()
  {a=new Pair [10];
   inputSize=0;
  }

  public void add(String word,int pos)
  { if(inputSize==a.length-1)
      {   Pair [] newA=new Pair [2*inputSize];
            for(int i=0;i<inputSize;i++)
               { newA[i]=a[i];}

               a=newA;
      }
    Pair p=new Pair(word,pos);

    for(int i=0;i<inputSize;i++)
     { if(a[i].getWord().equals(word))   // i check if "word" is already in the array, if so i add the new line
             { a[i].addPosition(pos);
             }
     }
    a[inputSize++]=p;
  }

  public String toString()
  {String s="";
   for(int i=0;i<inputSize;i++)
     { s=s+a[i].toString()+"\n";
     }
     return s;
  }




}

After I created the main class 我创建主类后

import java.io.*;
import java.util.*;

public class  IndixTester
{public static void main(String [] args)
  { FileReader read=null;
    Index index=new Index();
    Pair p;
    try{read=new FileReader("input.txt");
       }
    catch(IOException e)
       {System.err.println("errore");
       }
       int line=0;
     Scanner c=new Scanner(read);
     while(c.hasNextLine())
     { String s=c.nextLine();
        line++;
       Scanner token=new Scanner(s);
       token.useDelimiter("[^A-Za-z0-9]+");
       while(token.hasNext())
       {
         String ss=token.next();

         index.add(ss,line);


       }

       token.close();

     }
    System.out.println(index.toString());

  }
}  

But it doesn't work!I've got something like this : 但这不起作用!我有这样的东西:

  1. the [ 0 0] [0 0]

  2. a [ 0 0 0 0 0 ] a [0 0 0 0 0]

  3. dog [0 0 0] 狗[0 0 0]

Instead of having something like this 而不是像这样

  1. the [ 2 3 ] [2 3]
  2. a [ 12 4 3 5 ] 一个[12 4 3 5]
  3. dog [ 1 2] 狗[1 2]

Where ,of course , the numbers represent the lines that have that word. 当然,数字代表具有该词的行。 Please help me. 请帮我。 Thanks a lot 非常感谢

The problem is with your a[inputSize] in getPositions() function 问题a[inputSize]getPositions()函数中的a[inputSize]

  public String getPositions()
  {String s="";
   for(int i=0;i<inputSize;i++)
     {s=s+" "+a[inputSize] ;}
   return s;
  }

change it to a[i]: 将其更改为a [i]:

  public String getPositions()
  {String s="";
   for(int i=0;i<inputSize;i++)
     {s=s+" "+a[i] ;}
   return s;
  }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM