Initial Java library--arraylist

Source: Internet
Author: User

My car is a wheel, make a good wheel, I flew to the sky and the Sun side, think all Excited. What do you want to build your own wheels, is not silly, not all the shops are built by others, good and convenient, just a little money, you do not have money, then you can only be a quiet handsome Man. fortunately, the wheels in the programming world don't need money, so take a look at how to call the wheels in the library today.

Today's content:

I. Modifying a bugThis blog is next to the last article, do not read, please click here to view: the previous essay now reveals the biggest bug of the program: look at the test code, you may have guessed it. Look at the result: yes, you can continuously guess the same position to pass, see what is wrong, it must be in the Checkyourself method, look at the source code:
1  publicString Checkyourself (intGuess) {2String result = "miss";3          for(intI:localcells) {4             if(i = = Guess) {//right here, no handling after guessing .5result = "hit";6Numofhit + +;7                  break;8             }9         }Ten         if(numofhit = = 3) { oneresult = "kill"; a         } -         returnresult; -}

Now that you know the problem, you should solve it. You can have the following solutions.

1. Create another boolean[] Hitflag = {false, false, false}, guess where, set the position in the corresponding Hitflag array to true, and see if the status of the corresponding position in Hitflag is false after guessing, the successful , or guess, this failure.

This method is the most stupid, so the general smart you will not Think.

2. The number of the position can be set to 1, haha, the instant feeling of the world is good and beautiful ah. But why not take the Guess off? So just tell if your guess is in localcells.

3. If you have an array that can be scaled, it is good to remove unnecessary things from the inside, so that the program is running better and more. Do you have any? There really is one, there is a ArrayList in the Java library, which fits this requirement very well.

second, ArrayList: haha, His method is so perfectly adapted to our program, but you might think, if I don't say, where do you want to know these things, this I will say later this Question. Note a problem: ArrayList cannot be stored directly in primitive type, only objects can be saved, you can write a wrapper class if you have to save it, but after Java 5, the compiler can automatically convert the primitive type to a class. Now to modify the bug, just a little change the program just FINE.
Importjava.util.ArrayList; public classsimpledotcom {PrivateArraylist<string>localcells; //Private int[] dotCom; //private int numofhit = 0;     public voidSetdotcom (arraylist<string>Localcells) {         this. Localcells =localcells; }    //obviously much better.     publicstring checkyourself (string guess) {string result= "miss"; if(localcells.contains (GUESS)) {result= "hit";        Localcells.remove (guess); }        if(localcells.isempty ()) {result= "kill"; }        returnresult; }}

Test code:

1 ImportJava.util.*;2  public classTest {3          public Static voidmain (string[] Args) {4simpledotcom DotCom =Newsimpledotcom ();5Arraylist<string> Localcells =NewArrayList ();6Localcells.add ("1");7Localcells.add ("2");8Localcells.add ("3");9 dotcom.setdotcom (localcells);Ten                  oneString result = Dotcom.checkyourself ("1"); aSystem.out.println ("result =" +result); -                  -result = Dotcom.checkyourself ("1"); theSystem.out.println ("result =" +result); -                  -result = Dotcom.checkyourself ("2"); -System.out.println ("result =" +result); +                  -result = Dotcom.checkyourself ("3"); +System.out.println ("result =" +result); a                  at                  -         } -           -}

The result is correct: the simple version of the dotcom has been completed, do not forget our original intention oh, to write a full version of the dotcom, but the code is a little bit more and a bit complicated, to calm down to see, so jump to another essay, to see the full version of the Code bar. The following few words in the knowledge is relatively small, but I think it is good. The problem is that the classes in Java are in the package, and the advantages of using the newspaper are three Points:
    • Help you structure your project and avoid putting together a whole bunch of files.
    • To help you classify, such as the graphics library related apis, are in the javax.swing package, the event is related to the Java.awt.
    • Is that your program is more secure, only the files within the package are visible, and you can think of this feature as in C + + C # namespaces.

Explain a little bit more about why some package names have an "X", such as Swing's javax. The package named "java" was the first standard library, and "java" was later extended to the Java standard library.

You must use the Java class with its full name, for example, the system.out you are using. ArrayList you have to do this is to make,java.util.arraylist<string> list = new Java.util.arraylist<string> ();. But as you can see, you didn't write it because the import java.util.* in front of the file introduced everything under the Java.util package (not quite appropriate). You may wonder when you use string, you have nothing to import ah, very common classes are in java.lang, so Java.lang is already the default Package. If You've learned C + +, you might think that the more you import, the bigger your program is, the more you're wrong. Import Xxx. Do not put XXX under the things are copied in, just help you need the full name of XXX Place omitted this full name only. three, boolean-value ExpressionsTrue and False in Java is not like a Boolean value in C/s + +, true in C + + is an integer value of 1, false = = 0, and the conditional judgment will treat nonzero values as true, look at the C code below: result: The Boolean in Java itself is a type , the condition determines that only the Boolean type, the upper code if it is Java will certainly be wrong. Talk about powerful Boolean expressions. It's good to understand if You've learned C Language.
    • &&: expression 1 && expression 2 && ... , the result is true when each given expression is true, otherwise false. Such as: 1 = = 2 && 1 = = 1 result is false, 1 = = 1 && 2 > 1 is True.
    • | |: Expression 1 | | Expression 2 | | Expression 3 .... , when each expression has a true, the result is true, and the && is Reversed. Example: 1 = = 2 | | 1 = = 1 result is true, 1 < 0 | | 2 = = 1 is False
    • ! :! An expression that turns the result of an expression in reverse, such as! 1 = = 2, true,! false, True,!true to False
    • &: As with &&, this is not the same as c, but it is often used in binary and arithmetic and C Languages.
    • |: Same &

note:& and | and && | | The difference is that the expression 1 && expression 2, If the expression 1 results in false, then the expression 2 will not execute, and the & expression will be executed, | with | | With the previous Complaint.

Iv. Java API

API we encapsulate a lot of commonly used functions, avoiding the work of making wheels again. We just have to learn how to use the API (for our novice), for the small white we, how does I go from a need-to-do-something to a-way-to-do-it using the api, there are two common ways, one is to buy a Reference books, according to their own needs to find the desired api, reference books will provide you with the use of the API Syntax.

Another is the HTML document, free and aspect, strongly recommend it, than reference books in detail, and timeliness, the official at any time under the latest Documents.

One sentence per day:

It seems the harder I work, and the more luck I had.

The more you try, the luckier you Become.

Initial Java library--arraylist

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.