[英]Adding values to ArrayList elements of type class
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.
我正在尝试将名称值添加到indexStruct类型的ArrayList中,并且不知道如何执行此操作。 Name is a String, any help would be great.
Name是一个String,任何帮助都会很棒。 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);
}
}
I'm trying to add a name value to the ArrayList of type indexStruct.
我正在尝试将名称值添加到indexStruct类型的ArrayList。
The specific ArrayList can only have elements added that are of type indexStruct
. 特定的ArrayList只能添加
indexStruct
类型的indexStruct
。 This means, either they are: 这意味着,它们是:
The only way you could insert your string, is if there is a string field within the indexStruct Object. 您可以插入字符串的唯一方法是indexStruct对象中是否有字符串字段。
You would create such an object, and assign the name. 您将创建此类对象,并指定名称。 Then add this object to the arraylist.
然后将此对象添加到arraylist。
currentIndex.add(placeHolder, Word);
The problem with the call currentIndex.add(placeHolder, Word); //<--Where problem occurs
调用
currentIndex.add(placeHolder, Word); //<--Where problem occurs
currentIndex.add(placeHolder, Word); //<--Where problem occurs
, is that the Word
variable does not exist. currentIndex.add(placeHolder, Word); //<--Where problem occurs
,是Word
变量不存在。
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). 我假设您将currentWord与placeHolder中的特定元素进行比较,然后希望在它们相等时重新插入相同的Word(忽略大小写)。 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
. 您将基本上进入相同的另一个实例
indexStruct
在该位置的对象placeHolder
。
Also, I'd suggest you look into iterators
or enhanced for loops
which are optimized for iterating over ArrayLists
. 另外,我建议您查看
iterators
或增强的for loops
,这些for loops
针对迭代ArrayLists
进行了优化。
See: 看到:
You are using 您正在使用
You are getting issues because of the type of Indexstruct.word is not compatible with ArrayList. 由于Indexstruct.word类型与ArrayList不兼容,您遇到问题。
Try this Add currentIndex.add(placeHolder, new Indexstruct(word)); 试试这个添加currentIndex.add(placeHolder,new Indexstruct(word)); OR alternatively set word to newly constructed Indexstruct object and then add that object.
或者将word设置为新构造的Indexstruct对象,然后添加该对象。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.