使用Java和对象从vector获取数据的问题
我目前正在使用Java、JForms和Twitter4j库。尝试创建一种用tweetID填充向量的方法,以便对其执行操作(转发、收藏夹、回复等)。我对以下两个方面都有点问题: A.无法添加到向量,或 不能从中阅读 根据我从记录器输出中看到的情况,我肯定我没有正确地添加到阵列中。有人能和我一起看看发生了什么事吗 main.java(使用tweet ID添加到向量的代码) TwitterUtilities.java使用Java和对象从vector获取数据的问题,java,twitter4j,Java,Twitter4j,我目前正在使用Java、JForms和Twitter4j库。尝试创建一种用tweetID填充向量的方法,以便对其执行操作(转发、收藏夹、回复等)。我对以下两个方面都有点问题: A.无法添加到向量,或 不能从中阅读 根据我从记录器输出中看到的情况,我肯定我没有正确地添加到阵列中。有人能和我一起看看发生了什么事吗 main.java(使用tweet ID添加到向量的代码) TwitterUtilities.java public class TwitterUtilities { public Vec
public class TwitterUtilities {
public Vector tweetIDVector;
// Other stuff... but here's the important stuff.
public void setTargetID(long targetStatus)
{
this.tweetIDVector.addElement(targetStatus);
}
public long getTargetTweetID(int targetIndex)
{
// Get tweet ID for the selected tweet.
Object targetTweetID = this.tweetIDVector.get(targetIndex);
return (long) targetTweetID;
}
}
public class TwitterUtilities {
public Vector tweetIDVector = new Vector<>();
// More fun stuff...
public void setTargetID(long targetStatus)
{
this.tweetIDVector.addElement(targetStatus);
for (int i = 0; i <= tweetIDVector.size(); i++)
{
System.out.println(tweetIDVector.get(i));
}
}
运行时收到的错误消息:
Exception in thread "AWT-EventQueue-0" java.lang.ArrayIndexOutOfBoundsException: Array index out of range: 1
任何帮助都将不胜感激
编辑:使用建议后,我现在已将以下代码更新到此新设置:
TwitterUtilities.java
public class TwitterUtilities {
public Vector tweetIDVector;
// Other stuff... but here's the important stuff.
public void setTargetID(long targetStatus)
{
this.tweetIDVector.addElement(targetStatus);
}
public long getTargetTweetID(int targetIndex)
{
// Get tweet ID for the selected tweet.
Object targetTweetID = this.tweetIDVector.get(targetIndex);
return (long) targetTweetID;
}
}
public class TwitterUtilities {
public Vector tweetIDVector = new Vector<>();
// More fun stuff...
public void setTargetID(long targetStatus)
{
this.tweetIDVector.addElement(targetStatus);
for (int i = 0; i <= tweetIDVector.size(); i++)
{
System.out.println(tweetIDVector.get(i));
}
}
编辑2:将向量更改为列表。加载Tweet ID正在工作。根据我从日志中看到的情况,我确定我没有正确地添加到数组中。你从日志中看到了什么?为什么要隐藏信息?是否可能您没有初始化
tweetIDVector
?尝试使用vector tweetIDVector=new vector将其初始化为空向量代码>和矢量tweetIDVector=新矢量代码>。甚至试过一个ArrayList。仍然没有。更改了问题以便更好地解释我从何处读取错误。顺便说一句:
838641565769871360
Exception in thread "AWT-EventQueue-0" java.lang.ArrayIndexOutOfBoundsException: Array index out of range: 1