简体   繁体   English

Java 以前的数组值设置为 0

[英]Java Previous Array Values Set to 0

My issue is that the array called myMarks[];我的问题是名为myMarks[];的数组gets placement ( myMarks[increase] ) values set through the integer increase and the number to be set to is myMark which is inputted by the user.通过 integer increase设置的位置( myMarks[increase] )值,要设置的数字是用户输入的myMark

int increase = 0; //initializing var
int myMarks[];

private void ConfirmButtActionPerformed(ActionEvent evt) {
    // setting the size of the array to never end (first loop is 0+1 then 1+1, etc.)
    int myMarks[] = new int[increase + 1];

    // showing the size of the array
    System.out.println("Array length: " + myMarks.length);

    // getting inputted mark
    int myMark = Integer.parseInt(Mark.getText());

    myMarks[increase] = myMark;

    // checking value of increase each loop
    System.out.println("Position: " + increase);

    // so i can show the mathematically incorrect to user
    int forCosmetic = increase + 1; 

    // sets next line of MarkShow to the new array value
    MarkShow.append("Mark " + forCosmetic + ", " + myMarks[increase] + "\n");

    // showing value of myMarks
    System.out.println(myMarks[increase]);

    //Updating each loop
    increase++;
}

This is inside of a JFrame.这是 JFrame 的内部。

For example, if you the user inputted 50 for the array through the variable myMark, it would first show up in the array as: 50. The issue is when we continue the loop with the same value, the array myMarks is now: 0, 50. If we loop again it would be 0, 0, 50 and so on.例如,如果用户通过变量 myMark 为数组输入 50,它将首先在数组中显示为:50。问题是当我们以相同的值继续循环时,数组 myMarks 现在是:0, 50. 如果我们再次循环,它将是 0、0、50 等等。

I think what you're trying to do is change the size of an array but you're not doing it correctly.我认为您要做的是更改数组的大小,但您没有正确执行。 Please add comments / questions if I misunderstood your question.如果我误解了您的问题,请添加评论/问题。

To copy change an existing array, you must first allocate a new temporary copy.要复制更改现有数组,您必须首先分配一个新的临时副本。 Then you must copy all the elements of the old array into the new array (a shallow copy is almost always fine).然后您必须将旧数组的所有元素复制到新数组中(浅拷贝几乎总是可以的)。 Then you have to assign the new temporary copy to the old array.然后您必须将新的临时副本分配给旧数组。

(As Viswa commented, using List<Integer> would probably be better here. But if you must do this manually, this is the correct way to do it.) (正如 Viswa 评论的那样,在这里使用List<Integer>可能会更好。但如果您必须手动执行此操作,这是正确的方法。)

int increase = 0; //initializing var
int myMarks[];


private void ConfirmButtActionPerformed(java.awt.event.ActionEvent evt) {   

   //setting the size of the NEW array to never end (first loop is 0+1 then 1+1, etc.)
   int temp[] = new int[1+increase];  

   // IMPORTANT: must copy old array values
   System.arraycopy( myMarks, 0, temp, 0, myMarks.length );

   // now assign the temp copy so it replaces the original
   myMarks = temp;

   int myMark = Integer.parseInt(Mark.getText()); //getting inputted mark
   myMarks[increase] = myMark;
   int forCosmetic = increase + 1; 
   // ... other code...

   //Updating each loop
   increase++;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM