简体   繁体   English

将值添加到类型的ArrayList元素

[英]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: 这意味着,它们是:

  1. objects of class indexStruct. indexStruct类的对象。
  2. objects of a class extending indexStruct. 扩展indexStruct的类的对象。
  3. objects of a class implementing an interface called indexStruct. 实现名为indexStruct的接口的类的对象。

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 您正在使用

  1. ArrayList: Array List which can store objects of Indexstruct. ArrayList:Array List,可以存储Indexstruct的对象。
  2. And you are trying to store indexstruct.word (Probably a String as I guess). 而你正在尝试存储indexstruct.word(我猜可能是一个字符串)。

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.

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