Java 调用一个类';来自另一个类的方法

Java 调用一个类';来自另一个类的方法,java,Java,这是我当前的代码: 如何将这些方法调用到其他类 import java.util.Random; public class WordSelect { private Random r; public void selectHundred(String[]w5000, String[] w100) { r = new Random(); int i = 0; while (i < w100.length) {

这是我当前的代码:
如何将这些方法调用到其他类

import java.util.Random;

public class WordSelect {
    private Random r;

    public void selectHundred(String[]w5000, String[] w100) {
        r = new Random();     
        int i = 0;

        while (i < w100.length) {
            int random = r.nextInt(w5000.length);
            w100[i] = w5000[random];
            i++;
        }
    }
}
import java.util.Random;
公共类WordSelect{
私有随机r;
公共无效选择100(字符串[]w5000,字符串[]w100){
r=新随机数();
int i=0;
而(i
是否要创建WordSelect的对象并从其他类调用SelectHund方法?如果是这样,您可以执行以下操作

//This should be in the MainClass.java file
package test;

public class MainClass {
    public static void main(String[] args) {
        WordSelect ws = new WordSelect();


        String[] w5000 = new String[10];//replace this and the following 10 lines with your w5000

        w5000[0] = "a";
        w5000[1] = "b";
        w5000[2] = "c";
        w5000[3] = "d";
        w5000[4] = "e";
        w5000[5] = "f";
        w5000[6] = "g";
        w5000[7] = "h";
        w5000[8] = "i";
        w5000[9] = "j";



        String[] w100 = ws.selectHundred(w5000); // I believe you already have w5000, and you will the 100 random words in the w100
        for(int i=0;i<w100.length;i++) {
            System.out.println(w100[i]);
        }

    } 
}
//这应该在MainClass.java文件中
包装试验;
公共类主类{
公共静态void main(字符串[]args){
WordSelect ws=newwordselect();
String[]w5000=新字符串[10];//用w5000替换此行和以下10行
w5000[0]=“a”;
w5000[1]=“b”;
w5000[2]=“c”;
w5000[3]=“d”;
w5000[4]=“e”;
w5000[5]=“f”;
w5000[6]=“g”;
w5000[7]=“h”;
w5000[8]=“i”;
w5000[9]=“j”;
String[]w100=ws.selectcent(w5000);//我相信您已经有了w5000,您将在w100中找到100个随机单词

对于(int i=0;ijavascript不是java-编辑您的标记是否要选择100个唯一的随机项?如果是,那么您的逻辑将不得不更改。至于您的实际问题,我不确定是否“理解”帮你自己一个忙,帮助我们通过阅读帮助你让我试着重新解释,所以上面的代码应该从5000个单词中选择100个随机单词,我早就创建了处理5000个单词的其他类。100个选择的单词应该放在100个索引的数组中。你想知道如何返回t的数组吗他将100个字符串返回到调用模块?这是否回答了您的问题,还是您在寻找其他内容?我没有编译代码。我已替换为工作代码。只需替换w5000,这两个代码应位于单独的Java文件中。
//This should be in the WordSelect.java file
package test;

import java.util.Random;

public class WordSelect {
    private Random r;

    public String[] selectHundred(String[]w5000) {
        r = new Random();     
        int i = 0;
        String []w100 = new String [5];

        while (i < w100.length) {
            int random = r.nextInt(w5000.length);
            w100[i] = w5000[random];
            i++;
        }

        return w100;
    }
}