In JAVA, assign values to arrays dynamically and search for data in arrays. in java
Package java14.java;
/**
* Member operation
* @ Author Administrator
*
*/
Public class hydo {
Int num;
// Declare the array of member information
// HyInfo [] info = new hyInfo [5];
HyInfo [] info;
// Convert the array to dynamic
Public hyInfo [] Arraynum (int num ){
Info = new hyInfo [num];
Return info;
}
// Input the hyInfo array information
Public hyInfo [] info (hyInfo message ){
For (int I = 0; I <info. length; I ++ ){
If (info [I] = null ){
Info [I] = message;
Break;
}
}
Return info;
}
// Display the user
Public void show (){
System. out. println ("****** member list *****");
System. out. println ("# \ t points ");
For (int I = 0; I <info. length; I ++ ){
// If the lower-left mark of the object array is null, It is not printed.
If (info [I]! = Null ){
// Obtain the objects in the object Array
System. out. println (info [I]. hyid + "\ t" + info [I]. hynum );
}
}
}
/**
* Search for users
* @ Param name
* @ Return
*/
Public boolean searchName (String name ){
Boolean find = false;
For (int I = 0; I <info. length; I ++ ){
If (info [I]. hyid. equals (name )){
System. out. println ("the Member's points are:" + info [I]. hyid );
Find = true;
Break;
}
}
Return find;
}
}
Package java14.java;
/**
* Member Information
* @ Author Administrator
*
*/
Public class hyInfo {
String hyid; // member ID
String hynum; // Member points
}
Package java14.java;
Import java. util. collections;
/**
* Member information testing
* @ Author Administrator
*
*/
Public class Test {
// Main program entry
Public static void main (String [] args ){
// Instantiate keyboard input
Wrote input = new partition (System. in );
// Instantiate the member operation class
Hydo B = new hydo ();
System. out. print ("Enter the number of Members :");
Int num = input. nextInt ();
// Call a dynamic array
B. Arraynum (num );
// Instantiate member information class member number machine Member points
For (int I = 0; I <B .info. length; I ++ ){
// Instantiate the member Information Class
HyInfo a = new hyInfo ();
System. out. print ("Enter the member number :");
A. hyid = input. next ();
System. out. print ("enter member information :");
A. hynum = input. next ();
// Instantiate an array of member operation objects
HyInfo [] info = B .info ();
}
B. show (); // call the member operation class to display the user Method
For (;;){
// Instantiate keyboard input
Required input1 = new partition (System. in );
// Use Boolean to find the user name
System. out. print ("Enter the member ID to search :");
// Handle input exceptions
If (input. hasNextInt ()){
String name1 = input. next ();
// Determine whether the member information array exists.
If (B. searchName (name1 )){
// Exists
Break;
} Else {
// Does not exist
System. out. println ("This member does not exist! ");
Break;
}
} Else {// input exception handling error
Continue;
}
}
}
}
// Run the code