简体   繁体   English

将随机整数迭代到数组中

[英]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. 大家好,我正在尝试遍历数组,并用随机值填充每个Array元素。 the only problem is that i get weird outputs like 唯一的问题是我得到了奇怪的输出

[I@4b142196 [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; 您会得到一个奇怪的输出,因为数组的toString方法没有显示包含数组中各个成员的字符串。 instead, it shows a strange string starting in [I@ for arrays of primitive type int . 相反,它显示了一个以[I@开头的原始类型为int数组的奇怪字符串。

You should prepare the string manually, or call Arrays.toString(myArray) , like this: 您应该手动准备字符串,或调用Arrays.toString(myArray) ,如下所示:

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

The method generator.getRandomValue(); 方法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. 因此,如果您打印它,它将打印您的数组的String Representation ,您会看到它类似于数组引用的HashCode 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? 但是,为什么要追加然后将整数数组存储到String引用中呢?

If you want some random value out, then probably, you can use an ArrayList and use Collections.shuffle on that list to shuffle it. 如果您想要一些随机值,则可以使用ArrayList并在该列表上使用Collections.shuffle对其进行随机化。 If you want a single random value, then you can fetch the first value. 如果您想要一个随机值,则可以获取第first值。 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. 您会看到[I@4b142196字符串,因为这是数组的默认字符串表示形式。 (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 ... 如果要从数组中获取随机值,则实际上需要将一些随机性放入getRandomValue

You are adding an int[] to your JTextArea . 您正在将int[]添加到JTextArea It displays the result of calling toString() on your int[] which is the memory reference. 它显示在作为内存引用的int []上调用toString()的结果。

You should probably modify your getRandomValue() method to something like this: 您可能应该将getRandomValue()方法修改为如下形式:

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

You need to override toString method if you want the generated values. 如果需要生成的值,则需要重写toString方法。 Here in the code that you have wrote will give you the memory reference and not the generated values. 在这里,您编写的代码将为您提供内存引用,而不是生成的值。

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

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