package day08;
import java. util. Arrays;
public class NewForDemo {
public static void main ( String[ ] args) {
String[ ] array = { "one" , "two" , "three" , "four" } ;
for ( String s: array) {
System. out. println ( s) ;
}
}
}
package day08;
import java. util. ArrayList;
import java. util. Arrays;
import java. util. Collection;
import java. util. Iterator;
public class NewForDemo02 {
public static void main ( String[ ] args) {
Collection c = new ArrayList ( ) ;
c. add ( "one" ) ;
c. add ( "two" ) ;
c. add ( "#" ) ;
c. add ( "three" ) ;
c. add ( "four" ) ;
c. add ( 9.01 ) ;
Iterator i = c. iterator ( ) ;
for ( Object str: c) {
if ( "#" . equals ( str) ) {
}
System. out. println ( c) ;
}
}
}
package day08;
import java. util. ArrayList;
import java. util. Collection;
public class Demo {
public static void main ( String[ ] args) {
int a = 1 ;
String b = "abc" ;
Point p = new Point ( 1 , 2 ) ;
Collection c = new ArrayList ( ) ;
c. add ( p) ;
dosome ( a, b, p, c) ;
System. out. println ( "a:" + a) ;
System. out. println ( "b:" + b) ;
System. out. println ( "p:" + p) ;
System. out. println ( "c:" + c) ;
}
private static void dosome ( int a, String b, Point p, Collection c) {
a = a+ 1 ;
b = b+ "!" ;
p. setX ( 2 ) ;
p = new Point ( 3 , 4 ) ;
c. clear ( ) ;
c. add ( p) ;
c = new ArrayList ( ) ;
c. add ( new Point ( 5 , 6 ) ) ;
}
}
package day08;
import java. util. ArrayList;
import java. util. Collection;
public class CollectionDemo {
public static void main ( String[ ] args) {
Collection c = new ArrayList ( ) ;
boolean b = c. add ( "one" ) ;
c. add ( "two" ) ;
c. add ( "three" ) ;
c. add ( "four" ) ;
c. add ( "five" ) ;
System. out. println ( c) ;
int size = c. size ( ) ;
System. out. println ( "c集合的长度:" + size) ;
boolean empty = c. isEmpty ( ) ;
System. out. println ( empty) ;
c. clear ( ) ;
System. out. println ( "清空集合" ) ;
System. out. println ( c) ;
System. out. println ( "清空之后的集合长度" + c. size ( ) ) ;
System. out. println ( "清空之后是否为空" + c. isEmpty ( ) ) ;
}
}
package day08;
import java. util. ArrayList;
import java. util. Collection;
public class CollectionDemo02 {
public static void main ( String[ ] args) {
Collection c = new ArrayList ( ) ;
Point p= new Point ( 1 , 2 ) ;
c. add ( p) ;
System. out. println ( "p:" + p) ;
System. out. println ( "c:" + c) ;
p. setX ( 3 ) ;
System. out. println ( "p:" + p) ;
System. out. println ( "c:" + c) ;
}
}
package day08;
import java. util. ArrayList;
import java. util. Collection;
public class ConlicationDemo03 {
public static void main ( String[ ] args) {
Collection c = new ArrayList ( ) ;
c. add ( new Point ( 2 , 3 ) ) ;
c. add ( new Point ( 3 , 7 ) ) ;
c. add ( new Point ( 5 , 8 ) ) ;
c. add ( new Point ( 4 , 6 ) ) ;
System. out. println ( c) ;
Point p = new Point ( 5 , 8 ) ;
boolean boo = c. contains ( p) ;
System. out. println ( boo) ;
}
}
package day08;
import java. util. ArrayList;
import java. util. Collection;
public class CollectionDemo04 {
public static void main ( String[ ] args) {
Collection c = new ArrayList ( ) ;
c. add ( new Point ( 2 , 3 ) ) ;
c. add ( new Point ( 3 , 7 ) ) ;
c. add ( new Point ( 5 , 8 ) ) ;
c. add ( new Point ( 4 , 6 ) ) ;
System. out. println ( c. size ( ) ) ;
Point p = new Point ( 5 , 8 ) ;
c. remove ( p) ;
System. out. println ( "删除完毕" ) ;
System. out. println ( "删除后" + c. size ( ) ) ;
System. out. println ( c) ;
}
}
package day08;
import java. util. ArrayList;
import java. util. Collection;
public class CollectionDemo05 {
public static void main ( String[ ] args) {
Collection c = new ArrayList ( ) ;
c. add ( "java" ) ;
c. add ( "C#" ) ;
c. add ( "C++" ) ;
c. add ( "php" ) ;
System. out. println ( c) ;
Collection c1 = new ArrayList ( ) ;
c1. add ( "ios" ) ;
c1. add ( "addroid" ) ;
c1. add ( "hongmeng" ) ;
c. addAll ( c1) ;
boolean b = c. addAll ( c1) ;
System. out. println ( c) ;
c. removeAll ( c1) ;
System. out. println ( c) ;
Collection c2 = new ArrayList ( ) ;
c2. add ( "C#" ) ;
c2. add ( "C++" ) ;
c2. add ( "go" ) ;
c. removeAll ( c2) ;
System. out. println ( "c2" + c2) ;
System. out. println ( "删除后的c" + c) ;
}
}
package day08;
import java. util. ArrayList;
import java. util. Collection;
import java. util. Iterator;
public class CollectionDemo06 {
public static void main ( String[ ] args) {
Collection c = new ArrayList ( ) ;
c. add ( "one" ) ;
c. add ( "c#" ) ;
c. add ( "#" ) ;
c. add ( "two" ) ;
c. add ( "#" ) ;
c. add ( "three" ) ;
c. add ( "#" ) ;
c. add ( "four" ) ;
c. add ( "#" ) ;
System. out. println ( c) ;
Iterator it = c. iterator ( ) ;
while ( it. hasNext ( ) ) {
String str = ( String) it. next ( ) ;
if ( "#" . equals ( str) ) {
it. remove ( ) ;
}
System. out. println ( "遍历的数据有" + str) ;
}
System. out. println ( "删除完毕!" ) ;
System. out. println ( "删除后的集合:" + c) ;
}
}
package day08;
public class Point implements Comparable < Point> {
private int x;
private int y;
public Point ( int x, int y) {
super ( ) ;
this . x = x;
this . y = y;
}
public int getX ( ) {
return x;
}
public void setX ( int x) {
this . x = x;
}
public int getY ( ) {
return y;
}
public void setY ( int y) {
this . y = y;
}
@Override
public String toString ( ) {
return "Piont [x=" + x + ", y=" + y + "]" ;
}
@Override
public int hashCode ( ) {
final int prime = 31 ;
int result = 1 ;
result = prime * result + x;
result = prime * result + y;
return result;
}
@Override
public boolean equals ( Object obj) {
if ( this == obj)
return true ;
if ( obj == null)
return false ;
if ( getClass ( ) != obj. getClass ( ) )
return false ;
Point other = ( Point) obj;
if ( x != other. x)
return false ;
if ( y != other. y)
return false ;
return true ;
}
public int compareTo ( Point o) {
int len = this . x* this . x+ this . y* this . y;
int olen = o. x* o. x+ o. y* o. y;
return len- olen;
}
}