Skip to content
Advertisement

update attribute a element in arraylist on java?

I have a class

Class TextChunks extends Token {
    ArrayList<Token> arrt = new ArrayList<Token>();
}

extent fron class :

class Token {
    String s;
    int frequency = 1 ;// Tern frequency in TextChunk
    }

Now in token i have arraylist token , i want to update attribute frequency of token in Texchunks when have more than one tokens same .

For clearly a give a example :

Texchunks :” in particular in domain and range in some “

So have 8 token : in,particular,in,domain,and,range,in,some

i want update attribute frequency for token : in this example when i get attribute frequency of token “in” must return 3

it mean when i call : get frequency of Texchunks when dislay :

in 3
particular 1
in 3
domain 1 
and 1
range 1
in 3
some 1

here my code :

public TextChunks updateFrequencyOfTokenInTextChunks (TextChunks tc) throws CloneNotSupportedException {
        TextChunks result = (TextChunks) tc.clone();

        for (int i =0 ; i< result.arrt.size() ; i++ ){
            int j=i+1;
            if (result.arrt.get(i).compareTwoToken(result.arrt.get(j))== true )
            {
                // help here how to update attribute result.arrt.get(i) 
                            // and result.arrt.get(J) = ++ and 
            }
            
        }
        return tc;
        
    }

Here is method compare two token

    public boolean compareTwoToken(Token tk){
    if(this.s.toLowerCase().trim().equals(tk.s.toLowerCase()))
        return true;
    return false;
}

Advertisement

Answer

Your incomplete algorithm doesn’t work because j doesn’t find in previous position of i. A posible solution could be:

  • updateFrequencyOfTokenInTextChunks method:

    public static void updateFrequencyOfTokenInTextChunks (TextChunks tc) {
      Hashtable<String,Integer> visited = new Hashtable<String,Integer>();
    
      for (Token token : tc.arrt){
        if (visited.containsKey(token.s)) {
            token.frequency = visited.get(token.s);
        } else {
            int n = count(token, tc);
            visited.put(token.s, n);
            token.frequency = n;
        }
      }
    }
    

My solution doesn’t return any type, because I understand the update (updateFrequencyOfTokenInTextChunks) should modify the parameter (TextChunks tc) and no return a clone.

  • count auxiliar method:

    private static int count(Token t, TextChunks tc) {
      int cont = 0;
      for (Token token : tc.arrt) {
          if ( t.compareTwoToken(token) ) {
              cont++;
          }
      }
      return cont;
    }
    

Good luck!

User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement