集合的三种遍历方式/集合的嵌套/产生任意范围内的随机数

第一天:

1:需求:请设计一个方法,可以实现获取任意范围内的随机数。
package CollectionDemo_01;
import java.util.Scanner;
public class RandomDemo01 {
public static void main(String[] args) {
Scanner Sc=new Scanner(System.in);
System.out.println("请输入指定范围的开始数:");
int start=Sc.nextInt();
System.out.println("请输入指定范围的结束数:");
int end=Sc.nextInt();
int result =getNumber(start,end);
System.out.println("产生的随机数为:" + result);
}
public static int getNumber(int start,int end){
int number=(int)(Math.random()*(end-start+1))+start;
return number;
}
}


2:下面代码执行的结果是:
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));
}
}
####结果:false,true####


3:下面代码执行的结果是:
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;
}
####结果:AB,####
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);
System.out.println("str2 == " + str2);
####结果:This is a test!
      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
####结果:C####

第二天:

编程题
1:集合的嵌套遍历
  需求:
  我们班有学生,每一个学生是不是一个对象。所以我们可以使用一个集合表示我们班级的学生。ArrayList<Student>
  但是呢,我们旁边是不是还有班级,每个班级是不是也是一个ArrayList<Student>。
  而我现在有多个ArrayList<Student>。也要用集合存储,怎么办呢?
package CollectionDemo02_01;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;


public class CollectionText01 {
public static void main(String[] args) {
Collection<Collection<Student>> Cl=new ArrayList<Collection<Student>>();//创建父集合

Collection<Student> class1=new ArrayList();//创建第一个班的集合

Student s1=new Student(50,"王抢");
Student s2=new Student(31,"上官万成");

class1.add(s1);//添加第一个班的学生对象
class1.add(s2);

Cl.add(class1);//添加班级集合到年级集合中

Collection<Student> class2=new ArrayList();//创建第二个班的集合

Student s11=new Student(22,"欧阳");//添加第二个班中的学生对象
Student s12=new Student(26,"黄埔");

Cl.add(class2);//将第二个班的学生集合添加到年级集合中

Iterator<Collection<Student>> it1=Cl.iterator();//创建迭代器,遍历年级中的班级
int count1 = 1;
while(it1.hasNext()){
System.out.println("班级" + count1);
Iterator<Student> it2=it1.next().iterator();//再次创建迭代器,遍历班级一中的学生
while(it2.hasNext()){
Student p=(Student)it2.next();//向下转型,调用Student中的get()方法
System.out.println(p.getName() + "的年龄是:" + p.getAge());
}
Iterator<Student> it3=it1.next().iterator();//再次创建迭代器,遍历班级二中的学生
while(it3.hasNext()){
Student p1=(Student)it3.next();//向下转型,调用Student中的get()方法
System.out.println(p1.getName() + "的年龄为:" + p1.getAge());

}

// while(it1.hasNext()){
// //System.out.println("班级" + count1);
// Iterator<Student> it3=it1.next().iterator();
// while(it3.hasNext()){
// Student p1=(Student)it3.next();
// System.out.println(p1.getName() + "的年龄为:" + p1.getAge());
// }
// }
}


}
class Student{
private int age;
private String name;
public Student() {
super();
// TODO 自动生成的构造函数存根
}

public Student(int age, String name) {
super();
this.age = age;
this.name = name;
}


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;
}
}
        ######老师这个题我创建了个大集合,然后又建立了两个小集合,但是输出的时候,将两个小集合的迭代器放在大集合的迭代器中,没法输出,报异常呢!!!!!#####
2:获取10个1-20之间的随机数,要求不能重复
#####老师这一题不会做......#####
3:使用ArrayList集合存储自定义对象并遍历(三种方式去实现)
package ListText01;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
public class ArrayListDemo {
public static void main(String[] args) {
Collection Cl=new ArrayList();//创建集合对象


Person p1=new Person(22,"张三");//通过创建Person的对象,并传入参数赋值
Person p2=new Person(23,"李四");
Person p3=new Person(25,"王五");
Cl.add(p1);
Cl.add(p2);
Cl.add(p3);
System.out.println("通过Object的toArray()方法遍历数组:");
System.out.println("---------");
Object[] obj=Cl.toArray();
for(int i=0;i<obj.length;i++){
Person person=(Person)obj[i];//向下转型,将Object的引用强制转换为Person的引用
System.out.println(person.getName() + "的年龄为:" + person.getAge());
}
System.out.println("通过迭代器遍历:");
System.out.println("---------");
Iterator it=Cl.iterator();
while(it.hasNext()){
Person person1=(Person)it.next();//向下转型,
System.out.println(person1.getName() +"的年龄为:" + person1.getAge());
}
System.out.println("通过集合遍历:");
System.out.println("---------");
for(int i=0;i<Cl.size();i++){
Person per=(Person)((ArrayList) Cl).get(i);
System.out.println(per.getName() + "的年龄为:" + per.getAge());
}
}
}
class Person{
private int age;
private String name;
public Person() {
super();
// TODO 自动生成的构造函数存根
}

public Person(int age, String name) {
super();
this.age = age;
this.name = name;
}


public int getAge() {
return age;
}
public String getName() {
return name;
}
public void setAge(int age) {
this.age = age;
}
public void setName(String name) {
this.name = name;
}
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值