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
toStringmethod does not show the string containing individual members of the array; instead, it shows a strange string starting in[I@for arrays of primitive typeint.You should prepare the string manually, or call
Arrays.toString(myArray), like this: