簡體   English   中英

從另一個數組列表添加(連接)到數組列表的元素

[英]Add(concatenate) to elements of Arraylist from another arraylist

,我正在將一個arrayList與另一個arrayList進行比較,如果兩個元素中都“包含”的元素我想將第二個arraylist中的元素與第一個中的元素以及“ == neg ==“附加在一起。 問題是我要在第一個Arraylist中創建一個新元素,而不是將其附加到該元素。 我是編程新手,將不勝感激!

 public static List<String> neg_compare(  List<String>tagged, List<String>negative_words, List<String>total_compare )
        {
        //int pos_sentiment = 0;
        //int pos_count = 0;
        int total_tweets = 0;
        int neg_count = 0;


                for(int j =0 ; j < tagged.size(); j++)
                {
                    total_tweets ++;
                    total_compare.add(tagged.get(j));

                    for(int  k = 0; k < negative_words.size(); k++)
                    {
                        if(tagged.get(j).contains(negative_words.get(k)))
                        {

                            //pos_count ++;

                            total_compare.add( "   == neg == " + negative_words.get(k) +"\n");

                        }
                    }
                    System.out.print(total_compare + "\n");
                    System.out.print(total_tweets);
                }
                    return total_compare;
            }
}

您應該看一下.set(int index, E element)方法。 這將允許您用新元素替換舊元素,因此在您的情況下,可能會像這樣: total_compare.set(j, total_compare.get(j) + " == neg == " + negative_words.get(k) +"\\n");

    public static List<String> pos_compare(List<String> pos_words, List<String>sentiment,List<String>positive_tweets , List<String>negative_words )
    {

    int pos_count = 0;
    int total_tweets = 0;



            for(int j =0 ; j < sentiment.size(); j++)
            {
                total_tweets ++;

                //Search each tweet with words from pos.word
                for(int  k = 0; k < pos_words.size(); k++)
                {
                    if(sentiment.get(j).contains(pos_words.get(k)))
                    {

                        pos_count ++;

                    }

                }
                //Search for negative
                for(int i  = 0 ; i< negative_words.size() ; i++)
                {
                    if(sentiment.get(j).contains(negative_words.get(i)))
                    {

                        pos_count --;

                    }
                }
                if ((pos_count) > 0)
                {
                    positive_tweets.add(sentiment.get(j) + "positive" + "\n" );
                }
                else if((pos_count) == 0)
                {
                    positive_tweets.add( sentiment.get(j) +  "neutral" +"\n");
                }
                else if((pos_count) < 0)
                {
                    positive_tweets.add( sentiment.get(j) + "negative" + "\n");
                }


                pos_count = 0;
            }
            //System.out.print(positive_tweets + "\n");
            return positive_tweets;
    }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM