In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-26 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly shows you "JAVA how to achieve random roll call", the content is easy to understand, clear, hope to help you solve your doubts, the following let the editor lead you to study and learn "JAVA how to achieve random roll call" this article.
Special note: you can not directly output the collection or array objects, that is the address of the output, always output to the variables inside.
/ *
Random roll call, set improvement (student's name and age)
In reality, there are things like students, which use the form of defining classes to describe students' things.
Attribute: name, age
The name is stored in an array and the container is replaced with a collection
String [] s = {","}
Is the name of the student stored in the collection? The Student type should be stored
Storage students:
Student type, stored in collection
Overview: traversing collections
Random: random number, as an index, to find elements in the collection
Three functions, shared data, collection container
Define three methods that must pass a set of parameters
, /
Import java.util.ArrayList
Import java.util.Random
Public class CallName {
Public static void main (String [] args) {
/ / defines a collection that stores StudentName type variables
ArrayList array = new ArrayList ()
/ / call the add method
Add (array)
/ / call to traverse the collection
PrintArrayList (array)
RandomStudentName (array)
}
/ *
Random number, as the index of the collection, to find the element in the collection
, /
Public static void randomStudentName (ArrayList array) {
Random r = new Random ()
Int number = r.nextInt (array.size ())
/ / Random number, index, get to the collection
StudentName s = array.get (number)
System.out.println (s.name + "" + s.age)
}
/ *
An overview of students' information, traversing collections
, /
Public static void printArrayList (ArrayList array) {
For (int I = 0; I < array.size (); iTunes +) {
/ / when storing a collection, the collection .add (sn1) sn1 is a variable of type StudentName
/ / when getting, collect the .get method to get what is obtained, or a variable of type StudentName
StudentName s = array.get (I)
System.out.println (s.name + "" + s.age)
}
}
/ *
Define a method to store the name and age of the student
Create a variable of type StudentName and store it in the collection
, /
Public static void add (ArrayList array) {
/ / create a StudentName type variable
StudentName sn1 = new StudentName ()
StudentName sn2 = new StudentName ()
StudentName sn3 = new StudentName ()
StudentName sn4 = new StudentName ()
StudentName sn5 = new StudentName ()
Sn1.name = "Zhang San 1"
Sn1.age = 201
Sn2.name = "Zhang San2"
Sn2.age = 202
Sn3.name = "Zhang San3"
Sn3.age = 203
Sn4.name = "Zhang San4"
Sn4.age = 204
Sn5.name = "Zhang San5"
Sn5.age = 205
/ / store the StudentName variable in the collection
Array.add (sn1)
Array.add (sn2)
Array.add (sn3)
Array.add (sn4)
Array.add (sn5)
}
}
/ *
A class that describes students' things.
Attribute: name and age
Variables of student type, stored in the collection
, /
Public class StudentName {
String name
Int age
}
The above is all the contents of the article "how to achieve random roll call in JAVA". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.