Java 抓取数据类列表的随机索引
好的,我有这个Java 抓取数据类列表的随机索引,java,list,Java,List,好的,我有这个 public static List<DataValue> result = new LinkedList<>(); public static class DataValue { protected final String first; protected final int second; public DataValue(String first, int second) { this.first = f
public static List<DataValue> result = new LinkedList<>();
public static class DataValue {
protected final String first;
protected final int second;
public DataValue(String first, int second) {
this.first = first;
this.second = second;
}
}
我试图从数据中获取一个随机场,显示它,然后从列表中删除它,这样它就不会被再次使用
我该怎么做呢
我目前甚至试图获取数据,但效果并不理想
System.out.println(“First:+DataValue.result.getFirst())
也试过了
System.out.println(“First:+result.getFirst(DataValue))
我不知道如何抓住它,也找不到任何关于它的文章,任何帮助都是感激的
LinkedList中大约有5000个条目,如果这有什么区别的话我不太理解你的问题,但你可以试试这样
Random randomGenerator;
int randomIndex = randomGenerator.nextInt( result.size() );
DataValue dataValue = result.get( randomIndex );
//... Show the fields ...
result.remove( randomIndex );`
我不太明白你的问题,但你可以试试这样的
Random randomGenerator;
int randomIndex = randomGenerator.nextInt( result.size() );
DataValue dataValue = result.get( randomIndex );
//... Show the fields ...
result.remove( randomIndex );`
这个怎么样
import java.util.LinkedList;
import java.util.List;
public class MyClass {
public static List<DataValue> result = new LinkedList<DataValue>();
public static class DataValue {
protected final String first;
protected final int second;
public DataValue(String first, int second) {
this.first = first;
this.second = second;
}
@Override
public String toString(){
return first + " " + second;
}
}
public static void removeAndPrintRandom(){
int index = (int)(Math.random() * result.size());
System.out.println(result.remove(index));
}
}
import java.util.LinkedList;
导入java.util.List;
公共类MyClass{
公共静态列表结果=新建LinkedList();
公共静态类数据值{
首先保护最终字符串;
受保护的最后整数秒;
公共数据值(字符串第一,整数第二){
this.first=first;
这个秒=秒;
}
@凌驾
公共字符串toString(){
返回第一个+“”+第二个;
}
}
公共静态void removeAndPrintRandom(){
int index=(int)(Math.random()*result.size());
System.out.println(result.remove(index));
}
}
我刚刚向您展示了更好的方法。您可以根据需要编辑此文件并制作。这个怎么样
import java.util.LinkedList;
import java.util.List;
public class MyClass {
public static List<DataValue> result = new LinkedList<DataValue>();
public static class DataValue {
protected final String first;
protected final int second;
public DataValue(String first, int second) {
this.first = first;
this.second = second;
}
@Override
public String toString(){
return first + " " + second;
}
}
public static void removeAndPrintRandom(){
int index = (int)(Math.random() * result.size());
System.out.println(result.remove(index));
}
}
import java.util.LinkedList;
导入java.util.List;
公共类MyClass{
公共静态列表结果=新建LinkedList();
公共静态类数据值{
首先保护最终字符串;
受保护的最后整数秒;
公共数据值(字符串第一,整数第二){
this.first=first;
这个秒=秒;
}
@凌驾
公共字符串toString(){
返回第一个+“”+第二个;
}
}
公共静态void removeAndPrintRandom(){
int index=(int)(Math.random()*result.size());
System.out.println(result.remove(index));
}
}
我刚刚向您展示了更好的方法。你可以编辑它并根据你的要求制作。你可以粘贴你的stacktrace吗?没有stacktrace,添加很好,它捕获的数据不需要,我将删除一些不必要的代码这段代码中有些可疑,你的类在哪里保存loadList方法?datavalue类是保存数据的接口,其他内容都在main.java中。请尝试访问result.getFirst()。不需要DataValuse.result.GetFirst你能粘贴你的stacktrace吗?没有stacktrace,添加很好,它抓取了不需要的数据,我将删除一些不必要的代码这段代码中有些可疑,你的类在哪里保存loadList方法?datavalue类是保存数据的接口,其他内容都在main.java中。请尝试访问result.getFirst()。不需要DataValuse.result.GetFirst谢谢,需要DataValue DataValue=result行,欣赏它谢谢,需要DataValue DataValue=result行,欣赏它