Java array values being put into wrong array

corey

The following code runs once through the loop fine, but on the second pass, theEquation has had its data changed even though nothing had referenced it.

String[] theEquation = breakdown(theequation);
double[] yValues = new double[400];

for(int i=0; i < bitmapx; i++){
    Double v = xmin + (xstep * i);
    yValues[i] = Double.parseDouble( solveArrayX( theEquation , v ) );
}

For example, the first time through the for loop, theEquation will have { "x", "^", "2" }. The next time will be { previousCalculatedAnswer, null, null }

Why is theEquation being changed? No other code is referencing it.

aioobe

Why is theEquation being changed?

theEquation does not contain an array, it contains a reference to an array.

When you do solveArrayX( theEquation , v ) you're passing this reference to the solveArrayX method which changes the array. See lines 120 and 121:

public String solveArrayX(String[] tA, double d){
    ...

        tA[i] = tA[i+2];
        tA[i+2] = "";
    ...
}

If you want to avoid this, you can use Arrays.copyOf(theEquation, theEquation.length) as argument to the method.

この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。

侵害の場合は、連絡してください[email protected]

編集
0

コメントを追加

0

関連記事

分類Dev

Put all values of dict in an array

分類Dev

How to put values of objects from an array into a string

分類Dev

JAVA gui: array being modified unexpectedly

分類Dev

Appending session values to an Array in Java

分類Dev

Phoenix/Ecto - integer values in a Postgres array column not being retrieved

分類Dev

Getting values of all checked checkboxes in PHP and put them in an array

分類Dev

splitting string in a file and saving the values in an array is printing wrong output

分類Dev

Splitted Array being filled in for loop by function calculating values from a splitted array

分類Dev

Reading array values as user input gives wrong array length and only -a or -p works in read

分類Dev

How to put a string into an array

分類Dev

How to Put Thread into Array

分類Dev

Replacing values with for-loop in array in Java

分類Dev

How to get the values of generic array in Java?

分類Dev

I am displaying contents of a java array in reverse order of input, but only half the array is being displayed

分類Dev

Take an array of values and compare them to Enum values in java

分類Dev

Fix array not properly being assigned

分類Dev

Array is not being updated in useState hook?

分類Dev

Fill an Array With values from that array

分類Dev

Array into an array of objects in Java

分類Dev

Method not returning correct values for an array. What am I doing wrong?

分類Dev

Eclipse displays red values for a Java byte array when debugging

分類Dev

Checking if Integer is in Array Java does not return expected values

分類Dev

Adding up values around a specific point in an array in Java

分類Dev

Java Hash map / Array List Count distinct values

分類Dev

Misunderstanding of array_values

分類Dev

Spreading array values

分類Dev

Typescript enum values as array

分類Dev

Assigning values to a struct array

分類Dev

Search an array of values with Ransack

Related 関連記事

  1. 1

    Put all values of dict in an array

  2. 2

    How to put values of objects from an array into a string

  3. 3

    JAVA gui: array being modified unexpectedly

  4. 4

    Appending session values to an Array in Java

  5. 5

    Phoenix/Ecto - integer values in a Postgres array column not being retrieved

  6. 6

    Getting values of all checked checkboxes in PHP and put them in an array

  7. 7

    splitting string in a file and saving the values in an array is printing wrong output

  8. 8

    Splitted Array being filled in for loop by function calculating values from a splitted array

  9. 9

    Reading array values as user input gives wrong array length and only -a or -p works in read

  10. 10

    How to put a string into an array

  11. 11

    How to Put Thread into Array

  12. 12

    Replacing values with for-loop in array in Java

  13. 13

    How to get the values of generic array in Java?

  14. 14

    I am displaying contents of a java array in reverse order of input, but only half the array is being displayed

  15. 15

    Take an array of values and compare them to Enum values in java

  16. 16

    Fix array not properly being assigned

  17. 17

    Array is not being updated in useState hook?

  18. 18

    Fill an Array With values from that array

  19. 19

    Array into an array of objects in Java

  20. 20

    Method not returning correct values for an array. What am I doing wrong?

  21. 21

    Eclipse displays red values for a Java byte array when debugging

  22. 22

    Checking if Integer is in Array Java does not return expected values

  23. 23

    Adding up values around a specific point in an array in Java

  24. 24

    Java Hash map / Array List Count distinct values

  25. 25

    Misunderstanding of array_values

  26. 26

    Spreading array values

  27. 27

    Typescript enum values as array

  28. 28

    Assigning values to a struct array

  29. 29

    Search an array of values with Ransack

ホットタグ

アーカイブ