阵被覆盖在循环最后一个索引 [英] Array Being Overwritten with Last Index in Loop

查看:169
本文介绍了阵被覆盖在循环最后一个索引的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的工作code,它需要用绳子两个数组(字符串只是句),并将它们分配给它在另一个阵列召开类(在code下面显示的句子类数组)

因此​​,这里是我的问题。当弹出式列表()被调用时,循环贯穿两次,做工精细,把addStrings和addTranslation的第一个索引进入第一类中的数组中为止。然而,当循环指标和运行temp.sentence = addStrings [1]再次,它覆盖了一流的.sentence也。然后,当temp.translations = addTranslations [1]再次运行它覆盖了一流的.translation。

因此​​,通过在循环结束时,所有阵列的填充有同样的事情:addStrings和addTranslation的最后索引。每次循环它的时间将覆盖所有索引之前,用它应该在要放指数。

任何人都知道问题是什么吗?谢谢!

 公共类句{
公共字符串的句子;
公共字符串翻译;
句子(){
    一句话=;
    翻译=;
}
}    私人无效popStrings(){
    addStrings [0] =我是你的朋友。 addTranslations [0] =我是你的朋友。
    addStrings [1] =你可以帮助我吗? addTranslations [1] =你能帮助我吗?
    addStrings [2] =我不想吃啊!; addTranslations [2] =我不想吃!
}
//填充用绳子和翻译句子的数组的数组
私人无效弹出式列表(){
    INT I = 0;
    一句话临时=新句子();
    对于(i = 0; I< addStrings.length&放大器;&安培; I< addTranslations.length;我++){
        temp.sentence = addStrings [I]
        temp.translation = addTranslations [I]
        句子[I] =温度;
    }
}


解决方案

您需要创建新的句子()内循环:

 为(i = 0; I< addStrings.length&放大器;&安培; I< addTranslations.length;我++){
    一句话临时=新句子();
    temp.sentence = addStrings [I]
    temp.translation = addTranslations [I]
    句子[I] =温度;
}

否则,你在同一个对象连续设置的句子和翻译。

I'm working on code that takes two arrays with strings (the strings are just sentences) and allocates them to classes which are held in another array (The Sentence class array shown below in the code).

So here's my problem. When popList() is called, the for loop runs through twice and works fine, putting the first index of addStrings and addTranslation into the first class in the array. However, when the loop indexes up and runs temp.sentence = addStrings[1] again, it OVERRIDES the first class's .sentence also. Then when temp.translations = addTranslations[1] runs again it OVERRIDES the first class's .translation.

So by the end of the loop, all of the arrays are filled with the same thing: the last index of addStrings and addTranslation. Every time it loops it overwrites all the indices before it with the index it's supposed to be putting in.

Anyone know what the problem is here? Thanks!

public class Sentence {
public String sentence;
public String translation;
Sentence() {
    sentence = " ";
    translation = " ";
}
}

    private void popStrings() {
    addStrings[0] = "我是你的朋友。";  addTranslations[0] = "I am your friend.";
    addStrings[1] = "你可以帮助我吗?"; addTranslations[1] = "Could you help me?";
    addStrings[2] = "我不想吃啊!";   addTranslations[2] = "I don't want to eat!";
}
//Fill Sentence array with string and translation arrays
private void popList() {
    int i = 0;
    Sentence temp = new Sentence();
    for(i = 0; i < addStrings.length && i < addTranslations.length ; i++) {
        temp.sentence = addStrings[i];
        temp.translation = addTranslations[i];
        sentences[i] = temp;
    }
}

解决方案

You need to create new Sentence() inside the loop:

for(i = 0; i < addStrings.length && i < addTranslations.length ; i++) {
    Sentence temp = new Sentence();
    temp.sentence = addStrings[i];
    temp.translation = addTranslations[i];
    sentences[i] = temp;
}

Otherwise you set sentence and translation continuously in the same object.

这篇关于阵被覆盖在循环最后一个索引的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆