0

Here is what I have. I'm trying to add a name value to the ArrayList of type indexStruct and have no idea how to do that. Name is a String, any help would be great. Thanks!

import java.util.ArrayList;
public class Currentindex {


    public void indexedWords(String currentWord, ArrayList <Indexstruct> currentIndex){     
    int convoMax=currentIndex.size();
    int convoMin=0;
    int placeHolder;
    int strComp;
    //pull words to skip that appear frequently in a typical conversation


    while(convoMax>convoMin){           //binary search 
        placeHolder=(convoMin+convoMax)/2;
        strComp=currentWord.compareToIgnoreCase(currentIndex.get(placeHolder).Word);
        if(strComp==0){
        currentIndex.add(placeHolder, Word);  //<--Where problem occurs     
            break;
        }       
        else if(strComp<0){ 
        convoMax=placeHolder-1;     
        }   
        else{
        convoMin=placeHolder+1;
        }
    }
        //addterm(currentIndex);
        System.out.println(currentIndex);
    }
}

2 Answers 2

2

I'm trying to add a name value to the ArrayList of type indexStruct.

The specific ArrayList can only have elements added that are of type indexStruct. This means, either they are:

  1. objects of class indexStruct.
  2. objects of a class extending indexStruct.
  3. objects of a class implementing an interface called indexStruct.

The only way you could insert your string, is if there is a string field within the indexStruct Object.

You would create such an object, and assign the name. Then add this object to the arraylist.

currentIndex.add(placeHolder, Word);

The problem with the call currentIndex.add(placeHolder, Word); //<--Where problem occurs , is that the Word variable does not exist.

I am assuming that you compare the currentWord with the specific element at placeHolder, and then wish to re-insert the same Word Again when they are equal (ignoring case). This should work if you change your line to:

currentIndex.add(placeHolder, currentIndex.get(placeHolder));

You would essentially be entering another instance of the same indexStruct object at the position placeHolder.

Also, I'd suggest you look into iterators or enhanced for loops which are optimized for iterating over ArrayLists.

See:

Sign up to request clarification or add additional context in comments.

2 Comments

I'll give that a look and let you know how I get this working, thanks!
Which algorithm are you trying to implement?
0

You are using

  1. ArrayList: Array List which can store objects of Indexstruct.
  2. And you are trying to store indexstruct.word (Probably a String as I guess).

You are getting issues because of the type of Indexstruct.word is not compatible with ArrayList.

Try this Add currentIndex.add(placeHolder, new Indexstruct(word)); OR alternatively set word to newly constructed Indexstruct object and then add that object.

1 Comment

I think mine was a cheat but here is what I wound up doing Indexstruct allInfo=new Indexstruct(); allInfo.Word=currentWord; //pull words to skip that appear frequently in a typical conversation while(convoMax>convoMin){ //binary search parses through index of current words in ArrayList strComp=currentWord.compareToIgnoreCase(currentIndex.get(placeHolder).Word); if(strComp==0){ allInfo.numOfUses=currentIndex.get(placeHolder).numOfUses+1; currentIndex.set(placeHolder, allInfo); return; }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.