Java-11.4/11.5作业

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

package org.lemon.Test;

import java.util.Random;

public class TestDemo {
   public static void main(String[] args) {
	  int max = 100;
	  int min = 1;
	  //创建Random对象
	  Random random = new Random();
	  //定义一个数
	  int a = random.nextInt(max)%(max-min+1)+min;
	  
	  System.out.println(a);
	  
}
	}

2:下面代码执行的结果是:     false   true

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、下列代码的执行结果是:    str1 == This is a test!Hi      str2 == This is a test!Hi

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);
5:下面代码能最后打印的值是?   D
    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)nullC)0         D)1




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

package org.lemon.Test;
import java.util.ArrayList;
public class TestDemo {
   public static void main(String[] args) {
	 ArrayList<ArrayList<Student>> big = new ArrayList<ArrayList<Student>>();  
	 //一个
	 ArrayList<Student> a = new ArrayList<Student>();
	 Student s1 = new Student ("kobe",1);
	 Student s2 = new Student ("james",2);
	 Student s3 = new Student ("brown",3);
	 Student s4 = new Student ("tt",4);
	 
	 a.add(s1);
	 a.add(s2);
	 a.add(s3);
	 a.add(s4);
	 big.add(a);
	 //另一个
	 ArrayList<Student> b = new ArrayList<Student>();
	 Student s5 = new Student ("nash",5);
	 Student s6 = new Student ("fisher",6);
	 Student s7 = new Student ("odom",7);
	 Student s8 = new Student ("buynm",8);
	 b.add(s5);
	 b.add(s6);
	 b.add(s7);
	 b.add(s8);
	 big.add(b);
	 
	 for(ArrayList<Student>array : big) {
		 for(Student s :array) {
			 System.out.println(s.getAge()+"----"+s.getName());
		 }
	}
}
}
package org.lemon.Test;

public class Student {
	private String name ;
	private int age ;
	
	public Student() {
		super();
	}
	public Student(String name, int age) {
		super();
		this.name = name;
		this.age = age;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public int getAge() {
		return age;
	}
	public void setAge(int age) {
		this.age = age;
	}
	
	@Override
	public String toString() {
		return "Student [name=" + name + ", age=" + age + "]";
	}
}




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

package org.lemon.Test;

import java.util.ArrayList;
import java.util.Random;


public class TestDemo {
     public static void main(String[] args) {
	 
    	 ArrayList <Integer>  array = new ArrayList<Integer>();
    	 
	//创建Random对象
	 Random random = new Random();
	 int count = 0;
	 while(count<10) {
		 int number = random.nextInt(20)+1;
		 if(!array.contains(number){
			 array.add(number);
			 count++;
		 }
	 }	
	  for(Integer i : array) {
		System.out.println(i);
	}
		
	}
}




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

package org.lemon.List;
//用List集合存储字符串类型的元素,并遍历:迭代器遍历
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

public class ListDemo {
     public static void main(String[] args) {
		List list = new ArrayList();
		
		list.add("我");
		list.add("爱");
		list.add("你");
		
		Iterator it = list.iterator();
		while(it.hasNext()) {
			String s = (String) it.next();
		System.out.println(s);
		}
	}
}

//for的方法遍历list集合,使用size()和get()相结合
		for(int  x = 0 ; x < list.size(); x ++){
			Object object = list.get(x) ;
			//获取字符串内容同时,获取字符串长度
			String s = (String)list.get(x) ;
			System.out.println(s+"----"+s.length());
		}
//获取列表迭代器
	ListIterator it = list.listIterator() ;
		while(it.hasNext()){
			String s = (String) it.next();
		System.out.println(s);	
			
		}






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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值