随机读取数组

jan*_*giz 0 java

我有一个程序&我不会随机读取ARRAY我没有程序错误但在输出期间我有空值我能给你解决方案...

import java.io.FileInputStream;
import java.util.Properties;
import java.util.Random;
import javax.swing.JOptionPane;

public class ReadDB {
    public static void main(String[] args) {

        Properties prob = new Properties();
        String word [] = new String [20] ;
        try{
            prob.load( new FileInputStream("words.txt"));
        }catch(Exception e){
        }
        for(int i=1; i<6; i++){         
            String temp = prob.getProperty(""+i);
            word[i] = temp;
            Random ran = new Random ();

            String Val = word[ran.nextInt(word.length)];

            System.out.println(Val);

        }
    }
}
Run Code Online (Sandbox Code Playgroud)

Sam*_*lle 5

你的数组长度为20,你只填充5个值:for(int i=1; i<6; i++){.这解释了空值.