English 中文(简体)
提供意外结果的射线照射
原标题:Array access producing unwanted result

I am getting an unusual result when attempting to place a value in an array. I have an array table[] of a simple class result{ int score, long time, string ID} Intention is to have a sort of leader board. My code happily finds the correct place to insert a new score if it is in the top 10.

    int ix = 0; 
    int jx = 10; //
    while ( ix < jx )
    {
        if (points > sTable[ix].points)
        {
            // score is higher move records down
            for (jx = mNumRecords - 1; jx >ix ; jx--)
            {
                sTable[jx] = sTable[jx -1];
            }
            //now add new score
            sTable[ix].score = score; // all good until here
            sTable[ix].time = time;


        }

        ix++;           
    }

问题在于,当我试图用可衡量的[六]分点插入计分;

该数值为可兑换[ix]分,也为可兑换[ix +1]分。

重复的是,它以六分之一的价值发生,我通过该守则单行,只要我只能够告诉指挥员一次。

是否有人看到这一点?

问题回答

这是因为你抄录了标语中提及阵列的下一个要素。 您应复制价值,或提出新目标:

Option A:

// score is higher move records down
for (jx = mNumRecords - 1; jx >ix ; jx--)
{
    sTable[jx].time = sTable[jx -1].time;
    sTable[jx].score = sTable[jx -1].score;
}
//now add new score
sTable[ix].score = score; // all good until here
sTable[ix].time = time;

备选案文B:

for (jx = mNumRecords - 1; jx >ix ; jx--)
{
    sTable[jx] = sTable[jx -1];
}
sTable[ix] = new Result(score, time, ""); // Or however you construct the object




相关问题
In Eclipse, why doesn t "Show In" appear for non-Java files?

If I have a *java file open, I can right click on the source, scroll down to "Show In (Alt-Shift-W)", and select Navigator. If I have an *xml, *jsp, or pretty much anything besides a Java source ...

Eclipse: Hover broken in debug perspective

Since upgrading Eclipse (Galileo build 20090920-1017), hover in debug no longer displays a variable s value. Instead, hover behaves as if I were in normal Java perspective: alt text http://...

Eclipse smart quotes - like in Textmate

Happy Friday — Does anyone know if eclipse has the notion of smart quotes like Textmate. The way it works is to select some words and quote them by simply hitting the " key? I m a newbie here so be ...

Indentation guide for the eclipse editor

Is there a setting or plugin for eclipse that can show indentation guides in the editor? Something like the Codekana plugin for visual studio (not so fancy is also OK). Important that it works with ...

Adding jar from Eclipse plugin runtime tab

I want to add .jar files for plugin from the Runtime tab of manifest file. When I use the Add... button, I can see only sub-directories of the plugin project. So if I want to add same .jar file to ...

How to copy multiple projects into single folder in eclipse

I have two projects, Project1 and Project2. Now i want to copy this projects into eclipse workspace but i want these projects to be in a single folder like below. Eclipse Workspace -> Project -> ...

Java: Finding out what is using all the memory

I have a java application that runs out of memory, but I have no idea which code is allocating the memory. Is there an application with which I can check this? I use Eclipse.

热门标签