Java library -- ArrayList, java -- arraylist

Source: Internet
Author: User

Java library -- ArrayList, java -- arraylist

My car is just a wheel. When I build a wheel, I fly to the sky and the sun side by side. I'm so excited to think about it. Do you want to build your own wheels? Are you stupid? Isn't the store made by others? It's good and convenient. You only need a little money. If you don't have money, you can only be a quiet beautiful man. Fortunately, the wheel in the programming world does not need money. Today, let's look at how to call the wheel in the library.

Today's content:

  1. Modify bugsThis blog is followed by the previous article. If you have not read it, click here to view it: the previous article shows the biggest bug of the program: Check the test code, as you may have guessed. Check the result: Yes, you can guess the same position to pass the test continuously. Check the source code in the checkYourself method:
1 public String checkYourself (int guess) {2 String result = "miss"; 3 for (int I: localCells) {4 if (I = guess) {// here, no processing is done after the guess. 5 result = "hit"; 6 numOfHit ++; 7 break; 8} 9} 10 if (numOfHit = 3) {11 result = "kill"; 12} 13 return result; 14}

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

1. create another boolean [] hitFlag = {false, false, false}. to guess which position, set the position in the corresponding hitFlag array to true, after guessing, check that the status of the corresponding location in hitFlag is false. If the status is false, the operation is successful. Otherwise, the operation fails.

This method is the most stupid, so you will not think of it.

2. After a guess, you can set the number at that position to-1. Haha, it instantly feels wonderful in the world. But why don't I get rid of what I guess? So you only need to determine whether your guess is in localCells.

3. If there is an array that can be scaled, just remove unnecessary items from it. In this way, the program runs better and better. Is there any? There is actually an ArrayList in the java library that meets this requirement.

 

Ii. ArrayList: Haha, his method is so perfect to adapt to our program, but you may think, if I don't say, where do you want to know these things, I will talk about it later. Note the following: ArrayList cannot directly store the primitive type, but only objects.You can write a packaging class if you want to save it. However, after java 5, the compiler can automatically convert the primitive type to a class. Now, you only need to change the program a little.
Import java. util. arrayList; public class SimpleDotCom {private ArrayList <String> localCells; // private int [] dotCom; // private int numOfHit = 0; public void setDotCom (ArrayList <String> localCells) {this. localCells = localCells;} // much better public String CheckYourself (String guess) {String result = "miss"; if (localCells. contains (guess) {result = "hit"; localCells. remove (guess);} if (localCells. isEmpty () {result = "kill" ;}return result ;}}

Test code:

 1 import java.util.*; 2 public class Test { 3         public static void main(String[] args) { 4                 SimpleDotCom dotCom = new SimpleDotCom(); 5                 ArrayList<String> localCells = new ArrayList(); 6                 localCells.add("1"); 7                 localCells.add("2"); 8                 localCells.add("3"); 9                 dotCom.setDotCom(localCells);10                 11                 String result = dotCom.CheckYourself("1");12                 System.out.println("result = " + result);13                 14                 result = dotCom.CheckYourself("1");15                 System.out.println("result = " + result);16                 17                 result = dotCom.CheckYourself("2");18                 System.out.println("result = " + result);19                 20                 result = dotCom.CheckYourself("3");21                 System.out.println("result = " + result);22                 23                 24         }25          26 }

 

The result is correct: the simple version of DotCom has been completed. Don't forget our original intention. We need to write a full version of dotcom, but the code is a bit more complicated, to calm down and look at it, jump to another essay and see the full version of the code. The knowledge in the following sentence is relatively small, but I think it is good.To address this problem, classes in java all exist in packages. The advantages of using the reports are as follows:
  • Help you manage your projects in a structured manner and avoid putting a lot of files together.
  • Help you classify, for example, the APIS related to the graphics library are all in the javax. swing package, and the events are all in java. awt.
  • It is safer for your program. Only files in the package are visible between them. You can think that this function is the same as the namespace in c ++ c.

I would like to explain a little more why some package names have more "X", such as the javax of swing. The package named "java" is the standard library at the beginning, and "java" is later extended to the java standard library.

You must use the full name of the java class, for example, System. out. ArrayList you must make it in this way, java. util. ArrayList <String> list = new java. util. ArrayList <String> ();. But as you can see, you didn't write it like this, because the import java. util. * In front of the file introduces everything in the java. util package (not appropriate ). You may wonder if you didn't import anything when using String. Very common classes are in java. lang, so java. lang is the default package. If you have learned c/c ++, you may think that the more import, the larger your program is, then you are wrong. Import xxx. But I didn't copy all the things in xxx. I just saved the full name when I needed the full name of xxx. Iii. Boolean expressionTrue and false in java are not the Boolean values in c/c ++. true in c/c ++ is the integer value 1, false = 0, in addition, the condition judgment regards the non-zero value as true. Let's look at the C language code below: result: the boolean in java is a type, and the condition judgment only recognizes the boolean type, the above code is certainly wrong if it is java. A powerful Boolean expression. If you have learned the C language, it is easy to understand.
  • &: Expression 1 & Expression 2 &&..., if each expression is true, the result is true. Otherwise, the result is false. for example, the result of 1 = 2 & 1 = 1 is false, and 1 = 1 & 2> 1 is true.
  • |: Expression 1 | expression 2 | expression 3... when each expression is true, the result is true, which is opposite. For example, the result of 1 = 2 | 1 = 1 is true, and 1 <0 | 2 = 1 is false.
  • ! :! Returns the result of the expression, such! 1 = 2, true ,! False, true ,! True is false
  • &: Like &, This is different from the C language, but it is often used for binary and computation.
  • |: Same &

Note: & | and & | the difference is that expression 1 & Expression 2. If expression 1 returns false, expression 2 will not be executed, the & expressions are executed. | and | are the same as those mentioned above.

 

4. java API

API we encapsulate a lot of common functions to avoid repetitive work. We only need to learn how to use APIs (for new users, how do I go from a need-to-do-something to a-way-to-do-it using the API has two common methods: Buy a reference book, find the desired api based on your needs. The reference books will provide you with the api usage syntax.

The other is the html document, which is free and highly recommended. It is more detailed than the reference book and has a good timeliness. The latest documents are available on the official website at any time.

 

Daily sentence:

It seems the harder I work, the more luck I have.

The harder you work, the luckier you are.

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.