java作业

1:集合的嵌套遍历
需求:
我们班有学生,每一个学生是不是一个对象。所以我们可以使用一个集合表示我们班级的学生。ArrayList
但是呢,我们旁边是不是还有班级,每个班级是不是也是一个ArrayList。
而我现在有多个ArrayList。也要用集合存储,怎么办呢?
创建学生类

public class Student {
    private int age;
    private String 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;
    }
    public Student(int age, String name) {
        super();
        this.age = age;
        this.name = name;
    }
    @Override
    public String toString() {
        return "Student [age=" + age + ", name=" + name + "]";
    }


}
import java.util.ArrayList;
import java.util.Iterator;

public class ArrayListDemo {
    public static void main(String[] args) {
        ArrayList class1=new ArrayList();
        Student s1=new Student(20, "paul");
        Student s2=new Student(20, "kobe");
        Student s3=new Student(20, "james");
        Student s4=new Student(20, "allen");
        class1.add(s1);
        class1.add(s2);
        class1.add(s3);
        class1.add(s4);
        ArrayList class2=new ArrayList();
        Student s5=new Student(20, "paul1");
        Student s6=new Student(20, "kobe2");
        Student s7=new Student(20, "james3");
        Student s8=new Student(20, "allen4");
        class2.add(s8);
        class2.add(s7);
        class2.add(s6);
        class2.add(s5);
        ArrayList school=new ArrayList();
        school.add(class1);
        school.add(class2);
        Iterator it=school.iterator();
        while(it.hasNext()) {
            Object s= it.next();

            System.out.println(s);
        }

        }

}

这里写图片描述

2:获取10个1-20之间的随机数,要求不能重复

import java.util.Scanner;
import java.util.Random;
public class SuiJiShu {

          public static void main(String[] args){
          Scanner sc=new Scanner(System.in);
          System.out.println("请输入你想获取多少以内的随机值");
          int index=sc.nextInt();
          Random r=new Random();
          int a=r.nextInt(index+1);
          System.out.println("你获得的随机值为:"+a);
          }
        }

这里写图片描述

3:使用ArrayList集合存储自定义对象并遍历(三种方式去实现)

import java.util.ArrayList;
import java.util.Iterator;
public class ArrayListDemo1 {


        public static void main(String[] args) {
            Student s1= new Student(20,"paul");
            Student s2= new Student(20,"answer");
            ArrayList al=new ArrayList();
            al.add(s1);
            al.add(s2);
            //方法1

                    Iterator it=al.iterator();
                    System.out.print("[");
                    while(it.hasNext()) {

                        System.out.print(it.next()+", ");



                    }
                    System.out.println("]");
                    //方法二
                    Object[] arr=al.toArray();
                    for(int i=0;i<arr.length;i++) {

                    System.out.print(arr[i]+", ");

                    }
                    System.out.println();
                    //方法三

                    while(it.hasNext()) {
                        Student a=(Student)it.next();
                        System.out.print(a+",  ");
                    }


                }
        }

这里写图片描述
1:需求:请设计一个方法,可以实现获取任意范围内的随机数。

public class RandeomDemo{
  public static void main(String[] args){
  Scanner sc=new Scanner(System.in);
  System.out.println("请输入你想获取多少以内的随机值");
  int index=sc.nextInt();
  Random r=new Random();
  int a=r.nextInt(index+1);
  System.out.println("你获得的随机值为:"+a);
  }
}

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,ture

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,B

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 Text!Hi
This is a Text!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
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值