/**
*
*/
package org.zst.collection;
/**
* @author:zsyht
* @date:2017-11-9上午11:11:07
*/
public class Student {
private int age;
private String name;
public Student() {
super();
}
public Student(String name,int age){
this.name=name;
this.age=age;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
/** * */package org.zst.collection;/** * @author:zsyht * @date:2017-11-9上午11:07:33 */public class Demo { public static void main(String[] args) { //學生類創建完畢 //定義一個學生數組:定義數組的格式:數據類型[] 數組名稱 = new 數據類型[數組長度] ; //定義一個學生數組 Student[] students = new Student[5] ; //創建5個學生對象 Student s1 = new Student("yyb", 20) ; Student s2 = new Student("fyy", 20) ; Student s3 = new Student("mmw" ,20) ; Student s4 = new Student("lyq" ,20) ; Student s5 = new Student("zst" ,19) ; //賦值 students[0] = s1 ; students[1] = s2 ; students[2] = s3 ; students[3] = s4 ; students[4] = s5 ; //遍歷學生數組 for(int x = 0 ; x <students.length ; x ++){ //System.out.println(students[x]);//此時該語句輸出五個地址值 //通過底層原碼重寫toString()得到的每個對象的成員變量的信息 Student s = students[x] ; System.out.println(s.getName()+","+s.getAge()); } }}
public static void main(String[] args) {
//創建Collection集合對象
Collection c = new ArrayList() ;
//給集合中添加元素
c.add("高圓圓") ;//c.add(Object obj)=====>Object obj = new String("高圓圓") ;//向上轉型了
c.add("鄧超") ;
c.add("WE") ;
c.add("RNG") ;
//集合中有元素了,將集合轉換數組
Object[] objs = c.toArray() ;
//遍歷對象數組
for(int x = 0 ; x < objs.length ; x ++){
// System.out.println(objs[x]);
//需求:獲取集合中元素的同時,獲取集合中每一個元素的字符串長度
// System.out.println(objs[x]+"---"+objs[x].length());
//要獲取字符串長度:需要使用length(),該方法屬於String類的特有功能
String s = (String) objs[x] ;//向下轉型
System.out.println(s+"----"+s.length());
}
}
/**
*
*/
package org.zst.collection;
import java.util.ArrayList;
import java.util.Collection;
/**
* @author:zsyht
* @date:2017-11-9上午11:40:50
*/
public class CollectionDemo {
public static void main(String[] args) {
//創建一個Collection集合對象
//Collection c = new Collection() ;//JDK 不提供此接口的任何直接 實現:它提供更具體的子接口(如 Set 和 List)實現
Collection c = new ArrayList() ;
System.out.println(c);//[] 底層重寫了toString(),此時打印的不是地址值而是一個"[]".
//添加功能:
//給集合中添加指定的元素
boolean flag = c.add("hello") ;
/**
* 通過查看集合的add()方法,只要給集合中添加元素,永遠返回true
* public boolean add(E e) {
* //省略代碼
return true;
}
*/
c.add("hello") ;
c.add("world") ;
c.add("Java") ;
//void clear():刪除一個集合中的所有元素,暴力刪除,(不建議使用)
//c.clear() ;
//boolean remove(Object o):刪除一個集合中的指定元素
System.out.println("remove:"+c.remove("hello")) ;
System.out.println("remove:"+c.remove("javaweb")) ;
//boolean contains(Object o):判斷一個集合中是否包含指定的單個元素
System.out.println("contains:"+c.contains("world"));
System.out.println("contains:"+c.contains("android"));
//boolean isEmpty():判斷集合是否為空,如果為空,則返回true
System.out.println("isEmply:"+c.isEmpty());
//int size():獲取集合中的元素數
System.out.println("size:"+c.size());
System.out.println("c:"+c);
}
}
/**
*
*/
package org.zst.collection;
import java.util.ArrayList;
import java.util.Collection;
/**
* @author:zsyht
* @date:2017-11-9下午2:28:31
*/
public class CollectionDemo2 {
public static void main(String[] args) {
//創建兩個Collection集合對象
Collection c1 = new ArrayList() ;
Collection c2 = new ArrayList() ;
//分別給c1,c2集合添加元素
c1.add("abc1") ;
c1.add("abc2") ;
c1.add("abc3") ;
c1.add("abc4") ;
c2.add("abc4") ;
c2.add("abc5") ;
c2.add("abc6") ;
c2.add("abc7") ;
//boolean addAll(Collection c):添加一個集合中的所有元素
System.out.println("addAll:"+c1.addAll(c2));
//boolean removeAll(Collection c):刪除一個算是刪除
System.out.println("removeAll:"+c1.removeAll(c2));
//boolean containsAll(Collection c):包含所有算是包含
System.out.println("containsAll:"+c1.containsAll(c2));
//boolean retainAll(Collection c):思考:A集合給B集合做交集,交集的元素去哪里?返回值boolean表達什么意思?
/**
* 面試題:
* A集合對B集合取交集,那么交集的元素去A集合里面了,並且返回值boolean表達的意識是A集合中的元素是否發生變化,如果發生變化,就返回true;否則,false
*/
System.out.println("retianAll:"+c1.retainAll(c2));
//輸出每個集合中的元素
System.out.println("c1:"+c1);
System.out.println("c2:"+c2);
}
}
/**
*
*/
package org.zst.collection;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
/**
* @author:zsyht
* @date:2017-11-9下午3:16:15
*/
public class CollectionDemo4 {
public static void main(String[] args) {
Collection c = new ArrayList();//ArrayList是List的子實現類(存儲和取出一致)
c.add("hello");
c.add("world");
c.add("ssy");
Iterator it = c.iterator();
//Object obj = it.next();
//System.out.println(obj);
if(it.hasNext()){
System.out.println(it.next());
}
if(it.hasNext()){
System.out.println(it.next());
}
if(it.hasNext()){
System.out.println(it.next());
}
if(it.hasNext()){
System.out.println(it.next());
}
}
}
/**
*
*/
package org.zst.collection;
import java.util.ArrayList;
import java.util.List;
/**
* @author:zsyht
* @date:2017-11-9下午8:51:00
*/
public class ListDemo2 {
public static void main(String[] args) {
List list = new ArrayList();
list.add("ssy");
list.add("zst");
list.add("hello");
/* System.out.println("remove:"+list.remove(2));
System.out.println("list:"+list);*/
System.out.println("set:"+list.set(2,"isafoolishman"));
System.out.println("list:"+list);
}
}
本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系我们删除。