Package Cn.idcast2;import Java.io.bufferedreader;import Java.io.filenotfoundexception;import java.io.FileReader; Import java.io.ioexception;import java.util.arraylist;import java.util.random;/* Analysis: * A, the data stored in the text file in the collection * B, randomly generate an index * C, get a value based on the index * */public class Day26 {public static void main (string[] args) throws IOException {BufferedReader br = NE W BufferedReader (New FileReader ("A.txt")); arraylist<string> array = new arraylist<string> (); String line = null;while (line = Br.readline ())! = null) {Array.add (line);} Br.close ();//randomly generates an index of random r = new random (), int index = R.nextint (Array.size ());//Gets a value based on the index string name = Array.get ( index); System.out.println ("Lucky person is:" + name);}}
There is a text file in which several lists are stored, enabling random access to a person's name