In JAVA, assign values to arrays dynamically and search for data in arrays. in java

Source: Internet
Author: User

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

 

 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.