ArrayList set, arraylist

Source: Internet
Author: User

ArrayList set, arraylist

Reason why a set appears
Array Storage Data is a fixed storage. When the number of data to be stored is uncertain, the array is not satisfied, and the set appears.
The number of data stored in a set can change with the amount of data, which does not result in cross-border or a large amount of space waste.
The number of stored data is variable.

ArrayList:
Under the java. util package
An array is maintained at the underlying layer.
Threads are not synchronized (fast processing)

Format of the ArrayList object:
ArrayList <E> set name = new ArrayList <E> ();
<E>: generic type, which indicates the data type to be stored in the collection. If you want to store data of any type, change E to another type. If you want to store data of the String type, change E to String.


Note:: The set can only store referenced data.


Expression of the referenced data type corresponding to the basic data type
ByteByte
ShortShort
IntInteger
LongLong
FloatFloat
DoubleDouble
CharCharacter
BooleanBoolean

Common ArrayList Functions
Add
Public boolean add (E e)
Public void add (int index, E element) // add an element at the specified index location

Get Element
Public E get (int index) // obtain the element based on the index value


Obtain the number of elements
Public int size () // obtain the number of elements

Delete Element
Public boolean remove (Object o) // directly delete an element
Public E remove (int index) // delete an element based on the index and return the deleted Element

Modify Element
Public E set (int index, E element) // use element to replace the element of the specified index, and return the replaced element.

Student Management System exercises
Student information includes: Student ID, name, age, and hometown
Print welcome statement
Print the corresponding function and receive user input

1. View Student Information
If the system does not have student information, the corresponding prompt is displayed.
If the system contains student information, the student information is printed in the specified format.
2. Add Student Information
Add the student information component object entered on the keyboard to the Collection
Remove duplicates based on student IDs. Only student IDs that are not repeated can be added to the collection.

3. Modify Student Information
Locate the student based on the student ID and modify the student ID.
If no student ID exists, a corresponding prompt is displayed.
If you find the student ID, continue to collect new information and use the new information to modify the original element.
4. Delete Student Information
Delete student by student ID
If no student ID is specified, a specified prompt is displayed.
If a student ID exists, the specified element is deleted.
5. Exit the student information management system.
Prompt to exit
And end the program
Code demo

1 public static void main (String [] args) {2 // initialize data 3 // create a collection container to store Student information 4 ArrayList <Student> list = new ArrayList <Student> (); 5 // ================================== test data ========== ===================================== 6 // Student s1 = new Student ("9001 ", "ala jia", "18", "Dubai"); 7 // Student s2 = new Student ("9002", "ala yi", "18 ", "Dubai"); 8 // Student s3 = new Student ("9003", "ala pie", "18", "Dubai"); 9 // list. add (s1); 10 // list. add (s2); 11 // list. add (s3); 12 // System. out. println ("initialization completed "); 13 // ========================== test data ============ ===================================== 14 15 System. out. println ("------------------- welcome to the student management System ----------------------"); 16 17 // infinite loop 18 while (true) {19 // display function menu 20 System. out. println ("================================ "); 21 System. out. println ("1. view student information "); 22 System. out. println ("2. add student information "); 23 System. out. println ("3. modify student information "); 24 System. out. println ("4. delete student information "); 25 System. out. println ("5. log out of the student information management System "); 26 System. out. println ("Enter the serial number of the corresponding function"); 27 System. out. println ("================================ "); 28 // receive user input 29 bytes SC = new bytes (System. in); 30 int user = SC. nextInt (); 31 // call 32 switch (user) {33 case 1: 34 show (list); 35 break; 36 case 2: 37 add (list); 38 break; 39 case 3: 40 upd (list); 41 break; 42 case 4: 43 del (list); 44 break; 45 case 5: 46 System. out. println ("Thank you for using the Management System. Welcome to come back next time"); 47 // terminate the VM 48 System. exit (0); 49 // return; 50 break; 51 52 default: 53 System. out. println ("sorry, you do not have this function, please control yourself"); 54 break; 55} 56} 57} 58 59 // function method s 60 public static void del (ArrayList <Student> list) {61 // 1. the System prompts you to enter the student ID 62 student SC = new student (System. in); 63 System. out. println ("Enter your student id"); 64 String id = SC. next (); 65 66 // 2. search for 67 // definition mark 68 int index =-1; 69 // traverse and compare and modify 70 for (int I = 0; I <list. size (); I ++) {71 Student tmp = list. get (I); 72 if (tmp. getId (). equals (id) {73 // 74 found // changed the tag 75 index = I; 76 break; 77} 78} 79 // 3. judgment result 80 // judgment mark 81 if (index =-1) {82 // 83 System not found. out. println ("the student ID you entered does not exist in our system, please reselect the function"); 84} else {85 // find the 86 list to be deleted. remove (index); 87 System. out. println ("deleted"); 88} 89} 90 91 public static void upd (ArrayList <Student> list) {92 // 1. the System prompts you to enter the student ID 93 student SC = new student (System. in); 94 System. out. println ("Enter your student id"); 95 String id = SC. next (); 96 97 // 2. search for 98 // definition mark 99 int index =-1; 100 // traverse and compare 101 for (int I = 0; I <list. size (); I ++) {102 Student tmp = list. get (I); 103 if (tmp. getId (). equals (id) {104 // found 105 // modified the flag 106 index = I; 107 break; 108} 109} 110 // 3. perform different actions based on the search results. 111 // 112 if (index =-1) {113 // not found, 114 System. out. println ("the student ID you entered is not in our system, please reselect the function"); 115} else {116 // found 117 // 3. collect other information. 118 System. out. println ("enter a new name"); 119 String name = SC. next (); 120 System. out. println ("Please enter New age"); 121 String age = SC. next (); 122 System. out. println ("Enter your hometown"); 123 String home = SC. next (); 124 // 4. add the composition object to the set 125 Student s = new Student (id, name, age, home); 126 // modify 127 list. set (index, s); 128 System. out. println ("modified"); 129} 130 131} 132 133 public static void add (ArrayList <Student> list) {134 // 1. the System prompts you to enter the student ID 135 student SC = new student (System. in); 136 System. out. println ("Enter your student id"); 137 String id = SC. next (); 138 // 2. based on the student ID to go to 139 140 // use the student ID entered by the user to go to the collection for search. If the student ID that is the same as the student ID entered by the user is found to have repeated student IDs, you must continue to prompt for the student ID, and continue to remove weight 141 // collect other information when the student ID entered by the user is inconsistent with the student ID of the element in the Set 142 while (true) {143 // define a tag to a default value of 144 int index =-1; 145 // retrieve the student ID of the element from the traversal set and compare it with the student ID entered by the user 146 for (int I = 0; I <list. size (); I ++) {147 Student tmp = list. get (I); 148 if (tmp. getId (). equals (id) {149 // indicates repeated 150 // modify the flag 151 index = I; 152 break; 153} 154} 155 156 // judge to mark 157 if (index =-1) {158 // No duplicate 159 break; 160} else {161 // repeat 162 System. out. println ("the student id you entered already exists. Please enter another student id"); 163 id = SC. next (); 164 165} 166} 167 168 // 3. collect other information. 169 System. out. println ("enter your name"); 170 String name = SC. next (); 171 System. out. println ("Enter age"); 172 String age = SC. next (); 173 System. out. println ("Enter your hometown"); 174 String home = SC. next (); 175 // 4. add the composition object to the Collection 176 Student s = new Student (id, name, age, home); 177 list. add (s); 178 System. out. println ("added"); 179} 180 181 public static void show (ArrayList <Student> list) {182 // 1. determine whether the set has element 183 if (list. size () = 0) {184 // if no specific prompt is given 185 System. out. println ("no student information in the System, please select Add function"); 186} else {187 // if any, traverse the 188 System in the specified format. out. println ("===================== student information: = "); 189 System. out. println ("student ID \ t name \ t age \ t hometown"); 190 // retrieve student information from the traversal set 191 for (int I = 0; I <list. size (); I ++) {192 Student tmp = list. get (I); 193 System. out194. println (tmp. getId () + "\ t" + tmp. getName () + "\ t" + tmp. getAge () + "\ t" + tmp. getHome (); 195} 196 System. out. println ("======================================" ); 197} 198 System. out. println ("displayed"); 199}

 

Related Article

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.