如何从存储在接口文件中的数组中选择随机元素?

ha5z0ras  于 2021-07-09  发布在  Java
关注(0)|答案(2)|浏览(247)

所以我在写作业。我一直在尝试编写一个函数来选择一个随机元素。我尝试访问的数组存储在接口文件中。我有一个实现接口的主文件,然后有多个扩展主文件的文件。
我将编写并测试如何从接口文件中选择随机元素。该文件包含许多不同的数组,但出于测试目的,我将选择lastname[]。下面的函数是如何编写的,它抛出了一个越界错误,但我不知道如何去纠正它。
这是测试文件:

import java.util.Random;

public class MemberTest {

  public static void main(String[] args) {

    Random rnd = new Random();
    int rndNum = rnd.nextInt();

    Member m = new Member();
    Student s = new Student();
    Staff ss = new Staff();
    Faculty f = new Faculty();
    Employee e = new Employee();
    Names n = new Member();

    System.out.println(n.lastName[rndNum]);

    System.out.println(m.toString(true));
  }
}

这是实现names.java的文件

public class Member implements Comparable<Member>, Names{

public Member() {}

// randomly fill in all data member.
public void generate() {}

public String toString() {
    return toString(false);
}

public String toString(boolean ok) {
    return String.format("%s%3d-%2d-%4d %15s, %-15s", ok? "Mem " : "", ID / 1000000, ID / 10000
            % 100, ID % 10000, lastName, firstName );
}

//public int compareTo(Member) {}

public int compareTo(Member m) { 
    return ID - m.ID; 
}

public String htmlRow() {
    return String.format("<TR>%s</TR>", htmlColumns());
}

public String htmlColumns() {
    return String.format("<TD>%3d-%2d-%4d</TD> <TD>%15s,</TD> <TD>%-15s</TD>", ID / 1000000, ID / 10000
            % 100, ID % 10000, lastName, firstName );
} 

protected String firstName[], lastName[]; 
protected int ID;

}
这是接口文件:

public interface Names {

// Fields in interface are always and implicitly public, static and final.
// All methods in interface are implicitly public and abstract.

public static final String lastName [] = {
    "Abdalla", "Andres", "Anzai", "Armstrong", "Arvig", "Ash", "Baca", "Badine",
    "Baehr", "Bair", "Baligad", "Barlow", "Barrett", "Becker", "Bell",
    "Benbow", "Biggs", "Blevins", "Blood", "Bohan", "Bond", "Bonner",
    "Bosch", "Bryson", "Buechele", "Bullis", "Burk", "Burns", "Byron",
    "Calderon", "Careuthers", "Carr", "Carson", "Carter", "Castle", "Chan",
    "Charney", "Christenson", "Cisneros", "Cliton", "Coldewey", "Coodey",
    "Cook", "Cooper", "Coy", "Currie", "Curtis", "Dirkse", "Dirscoll",
    "Dodrill", "Ehlert", "Elias", "Elliott", "Enterline", "Ericsson",
    "Fernandez", "Fisher", "Flory", "Freese", "Fruzza", "Fu", "Fuhrman", "Gage",
    "Garcia", "Garmon", "Giffith", "Gill", "Gillen", "Glascock", "Gomez",
    "Goraya", "Greer", "Hansen", "Hartley", "Hawkins", "Hemme", "Hensley",
    "Hentges", "Herron", "Hightower", "Hines", "Holloway", "Holmes",
    "Issac", "Jackson", "Jagodin", "Jiang", "Johnson", "Jordan", "Jouda", "Joven",
    "Kalpesh", "Katz", "Kaur", "Kegley", "Kimsey", "King", "Kohn", "Kone",
    "LaCasse", "Lackey", "Lathrop", "Le", "Levy", "Lynos", "Maas", "Mackall",
    "Madsen", "Magee", "Maldonado", "McDaneld", "McMillian", "McNeill",
    "Meadors", "Medina", "Metz", "Millikin", "Minner", "Mondragon", "Monge", "Moore",
    "Moreno", "Murkland", "Musick", "Myers", "Neal", "Ngo", "Nolan", "Nunez",
    "O'Neil", "Oropeza", "Owen", "Patel", "Pfister", "Philips", "Picato",
    "Porras", "Porter", "Pratt", "Pryor", "Ragle", "Rai", "Raja", "Rill",
    "Roberts", "Roddy", "Rose", "Roux", "Rubio", "Ruiz", "Saito", "Samidin",
    "Sampson", "Sanchez", "Scanlan", "Schmidt", "Schoberg", "Sheppard",
    "Simmons", "Sison", "Smith", "Snow", "Soto", "Sozinho", "Stevens",
    "Stidham", "Strange", "Sy", "Tamayo", "Taylor", "Thomas", "Tran", "Vincent",
    "Walker", "Wandke", "Ward", "Warner", "Waugh", "Webb", "Wells", "White",
    "Wilson", "Winther", "Wuertz", "Yong" };
blmhpbnm

blmhpbnm1#

我怀疑这里的问题是测试代码中的这些行:

Random rnd = new Random();
int rndNum = rnd.nextInt();
...
System.out.println(n.lastName[rndNum]);

现在rnd实际上并不关心它的边界值是多少,或者这里的最大范围是多少。你得把它捆起来。所以,现在 rnd.nextInt() 可以生产100,1000,10000。。。
这就是你需要控制的
您可以做的是,将以下行重写为:

int rndNum = rnd.nextInt();

作为

int rndNum = rnd.nextInt()%lastName.length;

注意:我还没有读完全部代码,但是我看到的第一个问题是。试试看,如果还有问题就告诉我。

twh00eeo

twh00eeo2#

正如@himanshu所说的,如果你不能控制你的random的最大值,那么你的代码很容易因为arrayindexoutofboundsexception而失败
尝试更改获取整数的行: int rndNum = rnd.nextInt();int rndNum = rnd.nextInt(Names.lastName.length); 而且应该毫无问题地工作。

相关问题