需求:
我们班有学生,每一个学生是不是一个对象。所以我们可以使用一个集合表示我们班级的学生。ArrayList<Student>
但是呢,我们旁边是不是还有班级,每个班级是不是也是一个ArrayList<Student>。
而我现在有多个ArrayList<Student>。也要用集合存储,怎么办呢?
package _08.homework;
import java.util.ArrayList;
import java.util.Iterator;
public class CollectionSave {
public static void main(String[] args){
ArrayList arraylist=new ArrayList();
ArrayList classOne=new ArrayList();
classOne.add (new Student("白浅",160000,"女"));
classOne.add (new Student("夜华",70000,"男"));
classOne.add (new Student("团子",20000,"男"));
ArrayList classTwo=new ArrayList();
classTwo.add(new Student("墨渊",200000,"男"));
classTwo.add(new Student("东华",9000000,"男"));
classTwo.add(new Student("折颜",9000000,"男"));
arraylist.add(classOne);
arraylist.add(classTwo);
Iterator it = arraylist.iterator();
while(it.hasNext()){
System.out.println(it.next());
}
}
}
package _08.homework;
public class ArrayRandom {
public static void main(String[] args){
int Num = (int)((Math.random()*10+1)*2);
for(int i=0;i<10;i++){
System.out.print(" "+(int)((Math.random()*10+1)*2));
}
}
}
3:使用ArrayList集合存储自定义对象并遍历(三种方式去实现)
第一种
package _08.homework;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
public class ArrayListDemo {
public static void main(String[] args){
Collection c=new ArrayList();
Student s1=new Student("张三",22,"男");
Student s2=new Student("李四",28,"男");
Student s3=new Student("王兰",21,"女");
c.add(s1);
c.add(s2);
c.add(s3);
Iterator it = c.iterator();
while(it.hasNext()){
Student s = (Student)it.next();
System.out.println(s.getName()+"----"+s.getAge()+"----"+s.getSex());
}
}
}
第二种
package _08.homework;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
public class ArrayListDemo2 {
public static void main(String[] args){
List list = new ArrayList();
list.add("花好");
list.add("月圆");
list.add("人");
list.add("长久");
Iterator it= list.iterator();
while(it.hasNext()){
String s=(String)it.next();
System.out.print(s);
}
}
}
第三种
package _08.homework;
import java.util.ArrayList;
import java.util.Collection;
public class ArrayListDemo3 {
public static void main(String[] args){
Collection c= new ArrayList();
Student s1=new Student("佟掌柜",35,"女");
Student s2=new Student("白展堂",38,"男");
Student s3=new Student("莫小贝",12,"女");
Student s4=new Student("邢捕头",45,"男");
Student s5=new Student("吕秀才",24,"男");
Student s6=new Student("郭芙蓉",22,"女");
Student s7=new Student("李大嘴",32,"男");
c.add(s1);
c.add(s2);
c.add(s3);
c.add(s4);
c.add(s5);
c.add(s6);
c.add(s7);
Object[] objs=c.toArray();
for(int x=0;x<objs.length;x++){
Student s=(Student)objs[x];
System.out.println(s.getName()+"---"+s.getAge()+"---"+s.getSex());
}
}
}
1:需求:请设计一个方法,可以实现获取任意范围内的随机数。
2:下面代码执行的结果是 faise,trhue
public static void main(String[] args) {
String s1 = new String("hello");
String s2 = new String("hello");
System.out.print(s1 == s2);
System.out.print(",");
System.out.println(s1.equals(s2));
}
}
3:下面代码执行的结果是: AB,B
public static void main(String arg[]) {
StringBuffer a = new StringBuffer("A");
StringBuffer b = new StringBuffer("B");
operate(a, b);
System.out.println(a + "," + b);
}
static void operate(StringBuffer x, StringBuffer y) {
x.append(y);
y = x;
}
4:下面代码执行的结果是
6、下列代码的执行结果是:
String str1 = "This is a test!";
StringBuffer str2 =new StringBuffer( "This is a test!");
str1 = str1+"Hi";
str2.append("Hi");
System.out.println("str1 == " + str1); str1 == This is a test!Hi
System.out.println("str2 == " + str2); str2 == This is a test!Hi
7:下面代码能最后打印的值是?
public class TestValue {
private static int a;
public static void main(String[] args) {
modify(a);
System.out.println(a);
}
public static void modify(int a) {
a++;
}
}
A)编译错误 B)null C)0 D)1