Android Training (Java) day-container (bottom)

Source: Internet
Author: User

    1. Nextint (), nextline (), Next () the difference between the three:

    1. Nextine (): Input value with focus, nextline (): newline input, focus change, Next (): No newline input, focus unchanged.

    2. If Nextint () is placed first, the following two scenarios need to be considered:

      (1) Scanner s = new Scanner (system.in);

      System.out.println ("Please enter user name:");

      int a = S.nextint ();//has focus set in the same row;

      System.out.println ("Please enter password:");

      String B = s.nextline ();//Force line break, which loses focus, so cannot enter


    3. (2) Scanner s = new Scanner (system.in);


System.out.println ("Please enter user name:");

int A=s.nextint (); Has the focus set in the same row;

System.out.println ("Please enter password:");

String B = S.next (); This is the focus of the same line, so it can be entered;

Two common methods for 2.MAP interfaces:

KeySet (): The return type is a set interface, and the key value is placed inside the set container;

VALUES (): The return type is the collection interface, and the value is placed inside the collection container;


3.Arrays: Sort or search for an array

Common methods:

Sort (array type); From small to large sort;

ToString (array type); Returns a string type;


4.Collections: Sorting or searching for a container

Common methods:

Sort (list list); From small to large sort;

Shuffle (list list); Replaces the specified list with a default random source.

Reverse (list list); Reverses the elements in the container;


5. Enhanced content for generics:

Definition: Refers to the complexity of the type into uniqueness. Must be a reference type or a custom type.

There are four common methods:

(1) You can use < T > to represent any type, as long as the main method gives it a type;

(2) generic type can inherit:< T extends Collection, at this time in the method after the given type is immutable;

(3) wildcard character:<? Extends Collection, at this time in the main method can be given any one of the super-interface implementation class;

(4) How to use generics: You must precede the return value type with < T >; such as: public < T > void f (t x) {}

Commonly called methods: System.out.println (X.getclass (). GetName ());

Note: To understand the object


After-school exercises:

    1. Known: There is a two-dimensional array: int[][] a={{5,4},{3,2},{9,7}};

      To sort, the result is: 2,3,4,5,7,9;

      Knowledge points used: List interface, Collections class, two-dimensional array traversal;


Code:

public class Paixu {

public static void Main (string[] args) {

Int[][] a={{5,4},{3,2},{9,7}};

ArrayList ArrayList = new ArrayList ();

Traverse a two-dimensional array and put the results into the container ArrayList

For (int[] b:a) {

for (int i:b) {

Arraylist.add (i);

}

}

Collections.sort (arrayList); Sort the elements in ArrayList in ascending order

Iterate through the sorted ArrayList and output the elements individually

for (int j:arraylist) {

System.out.print (j);

if (J==arraylist.get (Arraylist.size ()-1)) {

Break

}

System.out.print (",");

}

}

}



2.

HashMap make an alternative, {1 = "xiaoming", 2 = "Small East", 3 = "Little West"}

"Xiao Ming" instead of "Xiao Red", and then the value in this, take out the reverse after, the West small, east small, red small


Code:

public class Replacer {

public static void Main (string[] args) {

hashmap<integer,string> HashMap = new hashmap<integer,string> ();

Hashmap.put (1, "xiaoming");

Hashmap.put (2, "Little East");

Hashmap.put (3, "Xiao Xi");

Hashmap.put (1, "Little Red"); To replace Xiao Ming as "Xiao Hong"

collection<string> value = Hashmap.values (); Remove value values

linkedlist<string> list = new linkedlist<string> ();

for (String Str:value) {

List.add (str);

}

Collections.reverse (Stringbuff);

StringBuffer stringbuff = null;

int i==0;

for (String str1:list) {

Stringbuff = new StringBuffer (STR1);

Stringbuff.reverse ();

System.out.print (Stringbuff);

if (I==list.size ()-1) {//through I to eliminate the end of the output of an extra ","

Break

}

System.out.print (",");

i++;

}

}

}








This article is from the "Kun" blog, please be sure to keep this source http://linyingkun.blog.51cto.com/2393912/1575923

Android Training (Java) day-container (bottom)

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.