java basis of Chapter 12. The collection, enhanced for loop, iterators and generics

Collection interface common method:
* All interfaces are sub-subclasses having
* a set of methods: CRUD
*
* public Boolean the Add (E E); // return value indicates whether the additive element added successfully
* public boolean remove (Object o); // remove elements, the return value indicates whether the deleted successfully
* no modification method
* method to query element is not, but
* public int size (); // number of elements in the collection of the set of queries
* // other methods :
* public void Clear (); // Clear set
* public Object [] toArray () ; // switch to set into an array
* @ determination method
* public boolean contains (Object o) ; // determines whether or not included in the collection specified element

collection is the interface, there are many subclasses under collection,
* subscripts have some subclasses, subclasses and some without subscripts, can not index to traverse
*
* by definition a collection subclass all the way through the collection common : iterator method
*
* collection using iterates over the set of steps:
*
* 1. define a set of objects (just collection subclass which can be)
*
* 2. .iterator collection object ()
*
* 3. Call iterator .hasNext () iterator .next ()
*
* 4. Step 3 is repeated until the iterator tell you false
*
* more way to use all Collection implementation class
*


* Enhanced for loop: foreach famous cycle
* format:
* for (data type of the variable name element: set / array) {
* syso (variable name);
*}
*
*
* enhanced for loop underlying using an iterator to achieve
*
*
* enhancement when for recycling ?
* when you only need to traverse to view data when using

public static void main (String [] args) {
// TODO Auto-Generated Method, Stub
// traverse the array
// int [] nums = {1,2,3 , } 4,5;
// for (int NUM: the nums) {
// System.out.println (NUM);
//}
Collection <Integer> = new new the nums the ArrayList <Integer> ();
nums.add (10);
nums.add (20 is);
nums.add (30);
nums.add (40);
// with the iterator. 1.
@ 2 for reinforcing loop.
for (Integer I: the nums) {
nums.add (50);
System.out.println (I);
}
// shortcut foreach + alt + /

* Use Iterator to loop through a collection of objects,
* if "baidu" string appears, then add an uppercase "baidu" string to the collection
*
* there was an exception:
* Concurrent (concurrent) Modification (modified) Exception: concurrent modification abnormal
* when you use iterates over the collection, if you add or remove elements to the collection during traversal
*
* iterator object gets out of time, has a memory function, record the number of elements in the set
* iteration of If you add process so memory length and different actual lengths


* generic: the type is an uncertain
* format: <E>, <P>, <Q>, <K>, <V>
*. 1. he is a security mechanism? (the transfer runtime to compile-time)
* 2. reduces the amount of code we
* avoid the cast
*
* we will use a lot of java generics defined in development
*
* but we rarely write own own use generics

to ArrayList <E> generics, generics <E> meaning
* E is a variable, waiting to receive a reference data types
*
* <E> use in java can be used on a class, an interface, the method
*
* 1. generic <E> With the class, java of ArrayList <E>

*
* 2. Generic <E> on the method used:
{....} public <T> return type method name (T t): * Format
* generic generic methods to determine when the call? to determine when to call once again determine the
* 3. generic <E> on the interface used: java in the Collection <E>
? * when a generic interface to determine generic
* 3.1 when the implementation class that implements the interface, the interface is determined generics
* 3.2 generics achieve uncertain, but the generic interface also inherited,
* the implementation class when creating the object determines

public class GenericDemo02 {

static void main public (String [] args) {
// the TODO Auto-Generated Method Stub
// generic interface. 3.
// MyClass2 <String> = MC2 of new new MyClass2 <String> ();
// mc2.show ( "ABC ");
MyClass2 <integer> = MC3 new new MyClass2 <integer> ();
mc3.show (12 is);
}
// find two integers, find the number of classes and the two float, double the number of the sum of two types and
public static <T> T SUM (num1 T, T num2) {
// return num1 num2 +;
return num1;
}

// public static SUM a float (num1 a float, a float num2) {
// return + num1 num2;
/ /}

/ *
* a generic method used
* /
public static void demo02 () {
// create a the Person
the Person <String> P = the Person new new <String> ();
p.show (123); // then transfer 123 this generic T is Integer
p.show ( "123"); // pass "123", then this is the generic type String T
}
/ *
* using generic class
* /
public static void demo01 () {
// generic class. 1. on
// the Person new new P = the Person ();
// p.setName (123); // will die because the parameter type is String
// the Person <String> P = the Person new new <String> ();
// p.setName ( "John Doe");
// String name = p.getName ();
the Person <Integer> P2 = the Person new new <Integer> ();
p2.setName (123);
Integer p2.getName I = ();
}

}


public class Person <E> {// This class is a generic class known as
E name;

public E getName() {
return name;
}

{// generic method used by the setName void class public (E name)
this.name = name;
}

public <T> T Show (T E) {// generic method
//System.out.println(e );
return E;
}

}

*
* Generics wildcards:? Any type
*
* complex format:
? * The extends String: // indicating the kind either String or String subclass
* the extends Animal:? //
*
* Super Animal:? // indicates that this types either Animal Animal either parent
* a member method in ArrayList <E> in:
* boolean addAll (<? the extends E> collection c)
*
* /

*
* 1. create a set of memory cards 54
2. shuffle * (the set of elements scrambled)
* 3. dealer (a person turns a)
* 4. the display card (individual card 3, card display landlord)
*
*
* /
public class DouDiZhuDemo {
public void main static (String [] args) {
// create a card. 1, to save the collection.
the ArrayList <String> = new new cards the ArrayList <String> ();
// add the card value card suit +
String [] nums = { "A ", "2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K"};
String [] = {Colors "♠", "♥", "♣", "♦"};
// color mosaic NUM +
for (String NUM: the nums) {
for (String Color : Colors) {
String Card Color = NUM +;
cards.add (Card);
}
}
cards.add ( "big S");
cards.add ( "small S");
. 2 // java provides a shuffling method Collections.shuffle (set);
Collections.shuffle (cards); // disrupted sequence of elements in a set
. licensing @ 3
@ define three sets
ArrayList <String> p1 = new ArrayList <String> ();
ArrayList <String> p2 = new ArrayList <String> ();
The ArrayList <String> P3 = new new the ArrayList <String> ();
// set of cards defined landlord
the ArrayList <String> new new DP = the ArrayList <String> ();
// iterate card 54
for (int I = 0; I <cards.size (); I ++) {
String = cards.get Card (I);
// if the last three are not sent, saved to the card set landlord
// i 53 51 is 52 is
IF (I> = 51 is) {
// does not send
dp.add (card);

} the else {
// 0. 1 p1 p1 P2 2 P3. 3
IF (I. 3% == 0) {
// p1 to licensing
p1.add (card);
} the else IF (I ==. 3. 1%) {
p2.add (card);
} the else {
p3.add (card);
}
}
}
. // display card. 4 (print all cards ) //
// call the method
lookCard (P1);
lookCard (P2);
lookCard (P3);
lookCard (DP);

}
// read the cards method
public static void lookCard (the ArrayList <String> cards) {
for (String card: Cards) {
of System.out.print (Card + "");
}
System.out.println();
}
}

Guess you like

Origin www.cnblogs.com/haizai/p/11071929.html