简体   繁体   中英

Iterate random integers into an Array

Hello everyone I am trying to Iterate trough an Array and fill each Array element with a random value. the only problem is that i get weird outputs like

[I@4b142196

the example for this I found on Array Example

but when using this in my code it won't work. Here is what I got:

package h05GrootsteWaarde;
import javax.swing.*;
import java.util.*;

public class GetallenGenerator extends JPanel {
    public GetallenGenerator() {
    }
    int[] val = new int[21];
    Random generator = new Random();    
    public void setRandomValue() {
        for (int i = 0; i < val.length; i++) {
            val[i] = generator.nextInt(201) - 100;
        }    
    }   
    public int[] getRandomValue() {     
        return val;     
    }
}

and here is how I call the function

package h05GrootsteWaarde;
import javax.swing.*;
import java.awt.event.*;
public class Bediening extends JPanel implements ActionListener {
    GetallenGenerator generator;
    private JButton bereken;
    private JTextArea veld;

    public Bediening(GetallenGenerator generator) {     
        this.generator = generator;     
        bereken = new JButton("Bereken kleinste");
        bereken.addActionListener(this);
        add(bereken);       
        veld = new JTextArea(13, 40);
        veld.setEditable(false);
        veld.setWrapStyleWord(true);
        veld.setLineWrap(true);
        add(veld);      
        generator.setRandomValue();
    }   
    public void actionPerformed ( ActionEvent e ) {     
        String hoi = " " + generator.getRandomValue();
        veld.append(hoi);       
    }
}

You get a strange output because array's toString method does not show the string containing individual members of the array; instead, it shows a strange string starting in [I@ for arrays of primitive type int .

You should prepare the string manually, or call Arrays.toString(myArray) , like this:

int[] arr = generator.getRandomValue();
String arrStr = Arrays.toString(arr);
String hoi = arrStr.substring(1, arrStr.length()-1);

The method generator.getRandomValue(); returns an integer array. So, if you print it, it prints the String Representation of your array, which you see is something like a HashCode for the array reference. So when you try to append it with an empty string and store it in another string, you will not get the desired result.

You can probably use is: -

String hoi = " " + Arrays.toString(generator.getRandomValue());

But why are you appending and then storing your integer array to a String reference?

If you want some random value out, then probably, you can use an ArrayList and use Collections.shuffle on that list to shuffle it. If you want a single random value, then you can fetch the first value. But that won't be very efficient way to get a random value. Because it will not be random enough.

The method:

public int[] getRandomValue() {
    return val;
}

... doesn't return a random value from the array - it returns the array reference itself. You're seeing the [I@4b142196 string because that's the default string representation of an array. (The first part indicates the type; the second part is a hash code.)

If you want to get a random value from the array, you'll need to actually put some randomness into getRandomValue ...

You are adding an int[] to your JTextArea . It displays the result of calling toString() on your int[] which is the memory reference.

You should probably modify your getRandomValue() method to something like this:

public String getRandomValue() {
  return Arrays.toString(val);
}

You need to override toString method if you want the generated values. Here in the code that you have wrote will give you the memory reference and not the generated values.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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