,我正在将一个arrayList与另一个arrayList进行比较,如果i中“包含”的元素想要将第二个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;
}
}
答案 0 :(得分:1)
您应该查看.set(int index, E element)
方法。这将允许您用新元素替换旧元素,因此在您的情况下,您可以这样:total_compare.set(j, total_compare.get(j) + " == neg == " + negative_words.get(k) +"\n");
答案 1 :(得分:0)
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;
}