向数组的正确位置添加字符串时出现问题(java)

向数组的正确位置添加字符串时出现问题(java),java,arrays,Java,Arrays,我正在做一个Instagram类型的项目,在那里你可以跟踪五个人。对于我的follow方法,我尝试循环遍历一个字符串数组(长度为5),并说如果“用户被跟踪”不在跟随者列表中,请将此人添加到数组中。但是当我这样做而不是像这样显示数组时(当我调用array.toString()时): 如下:[用户1、用户2、用户3、用户4、用户5] 它以如下方式显示阵列: 如下:[user1user2user3user4user5,,] 下面是我要说的代码: for (int i = 0; i < 5; i+

我正在做一个Instagram类型的项目,在那里你可以跟踪五个人。对于我的follow方法,我尝试循环遍历一个字符串数组(长度为5),并说如果“用户被跟踪”不在跟随者列表中,请将此人添加到数组中。但是当我这样做而不是像这样显示数组时(当我调用array.toString()时):

如下:[用户1、用户2、用户3、用户4、用户5]

它以如下方式显示阵列: 如下:[user1user2user3user4user5,,]

下面是我要说的代码:

for (int i = 0; i < 5; i++) {
  if(...) {
    this.following[i] += gettingFollowed.getHandle();

/* getHandle() gets the name of the user getting followed and adds it to the string array, 'following' */
for(int i=0;i<5;i++){
如果(…){
this.following[i]+=gettingfollowind.getHandle();
/*getHandle()获取被跟踪用户的名称,并将其添加到字符串数组“following”*/

this.following[i]+=gettingfollowind.getHandle();

+=
指令附加到字符串-这就是您看到输出的原因。您将所有用户附加到同一索引

this.following[i]=gettingfollowind.getHandle();

只需删除“+”即可设置该值


但是,在不知道.getHandle()的作用的情况下-这可能无法完全修复代码。

为什么不只包含代码?如果问题是由
引起的,那么很难调试该问题如果(…)
您正在编写的某些代码请为getHandle()添加代码部分。需要查看返回值。