Java oop題3(重要5題):ArrayList集合對象,對象放入集合,只能存放,添加,修改

知識延伸:ArrayList
該類也是實現(xiàn)了List的接口,實現(xiàn)了可變大小的數(shù)組,隨機訪問和遍歷元素時,提供更好的性能。該類也是非同步的,在多線程的情況下不要使用。ArrayList 增長當(dāng)前長度的50%,插入刪除效率低。




//1、創(chuàng)建一個ArrayList集合對象,在該集合中添加8種基本數(shù)據(jù)類型對應(yīng)的包裝類型,。通過循環(huán)遍歷打印出所有對象。
package a;
import java.util.ArrayList;
import java.util.Iterator;
public class jihe {
public static void main(String[] argss) {
ArrayList s = new ArrayList();
Byte A1 = 5;
Integer A2 = 20;
Float A4 = (float) 1314;
Long A3 = (long) 9.9;
Character A5 = 'a';
String A6 = "anan";
Boolean A7 = true;
Double A8 = 520.0;
s.add(A8);
s.add(A7);
s.add(A6);
s.add(A5);
s.add(A4);
s.add(A3);
s.add(A1);
for (int i=0; i<s.size(); i++) {
System.out.println(s.get(i));
}
}
}

// 2、創(chuàng)建一個ArrayList集合對象,創(chuàng)建五個不同的對象Apple, Banana,將這些對象放入集合中,并通
// 過循環(huán)遍歷打印出來。
package a;
import java.util.ArrayList;
import java.util.Iterator;
public class jihe {
public static void main(String[] args) {
ArrayList s = new ArrayList();
shangping A1 = new shangping("Apple");
shangping A2 = new shangping("Banana");
shangping A3 = new shangping("AK47");
shangping A4 = new shangping("AWM");
shangping A5 = new shangping("98k");
s.add(A1);
s.add(A2);
s.add(A3);
s.add(A4);
s.add(A5);
for (int i = 0; i < s.size(); i++) {
System.out.println(s.get(i).toString());
}
}
}class shangping{
String name ;
public shangping(String name){
// TODO Auto-generated constructor stub
this.name=name;
}
@Override
public String toString() {
return "商品名:" + name;
}
}

//3、創(chuàng)建一個只能存放Apple的ArrayList,通過循環(huán)添加6個Apple對象。然后通過循環(huán)遍歷打印出來
package a;
import java.util.ArrayList;
import java.util.Iterator;
public class jihe {
public static void main(String[] args) {
ArrayList<Apple> s = new ArrayList();
for (int i = 0; i < 6; i++) {
s.add(new Apple(i));
}
for (int i =0; i <s.size(); i++) {
System.out.println(s.get(i).ID);
}
}
}
class Apple {
int ID;
public Apple(int ID) {
// TODO Auto-generated corr'structor stub
this.ID = ID;
}
public int getID() {
return ID;
}
}

// 1.創(chuàng)建一個學(xué)生類,包含學(xué)生編號,姓名,性別,成績,將學(xué)生放入類集合中,添加5名學(xué)生,算出學(xué)生的平均成績
package a;
import java. util. ArrayList;
import java. util. Iterator ;
public class jihe{
public static void main(String[] arrgs) {
ArrayList s=new ArrayList();
Studic A1=new Studic("5_20", "詩名",'男',95);
Studic A2=new Studic("1_31", "書名",'男',92);
Studic A3=new Studic("4_52", "畫名",'男',90);
Studic A4=new Studic("2_22", "唱名",'男',111);
Studic A5=new Studic("6_66", "帥名",'男',134);
s. add(A1) ;
s. add(A2) ;
s. add(A3) ;
s. add(A4) ;?
s. add(A5) ;
double sum=0;
for(int i=0;i<s.size();i++) {
Studic k=(Studic)s.get(i) ;?
sum+=k.zj;}
System. out. println("學(xué)生的平均成績:"+sum/s.size());
}}
class Studic{
String ID;
String name;
char sex ;
double zj;
public Studic (String ID, String name, char sex, double zj) {
this. ID=ID;
this. name=name ;
this. sex=sex;
this. zj=zj;
}
}

// 2.在第一題的基礎(chǔ)上,讓用戶輸入學(xué)生編號,修改學(xué)生的成績
package a;
import java. util. ArrayList;
import java. util.Iterator ;
import java. util. Scanner;
public class jihe{
public static void main(String[] args) {
ArrayList s=new ArrayList() ;
Studic A1=new Studic("5_20", "詩名",'男',95);
Studic A2=new Studic("1_31", "書名",'男',92);
Studic A3=new Studic("4_52", "畫名",'男',90);
Studic A4=new Studic("2_22", "唱名",'男',111);
Studic A5=new Studic("6_66", "帥名",'男',134);
s. add(A1) ;
s. add(A2) ;
s. add(A3) ;
s. add (A4);
s. add(A5) ;
for(int i=0;i<s. size() ;i++) {
System. out. println(s.get(i).toString());}
Scanner sc=new Scanner (System. in);
System. out.println("請輸入學(xué)生編號");
String bh=sc.next();
System.out.println("請輸入修改的成績");
double xg=sc.nextDouble() ;
for(int i=0;i<s.size();i++){
Studic k= (Studic)s.get(i) ;
if(k. ID. equals (bh)) {
k. zj=xg;
}
}
double sum=0;
for(int i=0;i<s.size();i++) {
Studic k=(Studic)s.get(i) ;?
sum+=k.zj;}
System. out. println("學(xué)生的平均成績:"+sum/s.size());
}}
class Studic{
String ID;
String name;
char sex;
double zj;
public Studic(String ID, String name, char sex, double zj) {
// TODO Auto-generated constructor stub
this. ID=ID;
this. name=name ;
this. sex=sex;
this. zj=zj;
}
@Override
public String toString() {return"編號:"+ ID + "\t名字:"+ name +"\t性別:"+ sex + "\t成績:"+ zj;
? }
}
