基本数据类型对应的包装类,ArrayList和LinkedList小练习

 练习一:

package test;

public class Test1 {

	public static void main(String[] args) {
		//计算两个正数相加的和
//		int a = 5;
//		int b = 6;
//		int sum = a+b;
//		System.out.println(sum);
		
		int a = 5;
		String b = "6"; //把字符串变成正数型
		int sum = a + Integer.parseInt(b); //通过int的包装类Integer的parsInt将字符串转换成整型
		System.out.println(sum);
		
		String c = "6.8";
		double c1 = Double.parseDouble(c); //通过double的包装类Double的parseDouble将字符串转换成浮点型
		System.out.println(c1 + 4.3);
		
		String d = "true";
		boolean e = Boolean.parseBoolean(d); //通过boolean的包装类Boolean的parseBoolean方法将字符串转换成布尔类型
		if(e) { //if(e == true)
			System.out.println("哈哈哈");
		}
		
		String f = "null";
		System.out.println(Integer.parseInt(f) + 8);
		
		int g = 8; //创建一个基本数据类型的变量,存储的内容是一个正数
		Integer g = 8; //创建了一个引用数据类型的变量,储存的内容是一个正数
		System.out.println(g.intValue()); //获得包装类对象内存储的内容
		
		Double h = 8.8;
		System.out.println(h.doubleValue());
	}

}

练习二:

要求:使用ArrayList保存学生信息(学号,姓名,年龄,成绩),要求删除指定学生后遍历输出。
(1) 正确创建集合类对象。
(2) 创建三个学生对象。
(3) 将学生对象保存到集合。
(4) 删除一个对象。
(5) 遍历集合。

(6)求平均分和总分
(7) 正确输出结果。

package test1;

public class Student {

	private String name;
	private double score;
	
	public Student(String name, double score) {
		this.name = name;
		this.score = score;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public double getScore() {
		return score;
	}

	public void setScore(double score) {
		this.score = score;
	}

}
public void test4() {
		//创建3个Student对象,并且实例化
		Student stu1 = new Student("张三", 68);
		Student stu2 = new Student("李四", 78);
		Student stu3 = new Student("王五", 99);
		
		//创建ArrayList集合对象
		List<Student> stus = new ArrayList<Student>();
		stus.add(stu1); //将学生对象保存到集合容器中
		stus.add(stu2); //将学生对象保存到集合容器中
		stus.add(stu3); //将学生对象保存到集合容器中
		double sum = 0; //求和
		double avg = 0; //求平均分
		
		//用普通for循环求和
//		for(int i=0; i<stus.size(); i++) {
//			Student s = stus.get(i); //遍历得到当前学生对象
//			sum = sum + s.getScore(); //sum += s.getScore();
//		}
		//用增强型for循环求和
		for(Student s : stus) {
			sum = sum + s.getScore(); //sum += s.getScore();
		}
		avg = sum / stus.size();
		System.out.println("和是:"+sum);
		System.out.println("平均分是:"+avg);
	}

练习三:

题目需求:
实现使用ArrayList集合容器存储数据并遍历。
需求分解:
(1)定义一个U盘类,包含U盘品牌,价格,封装该类的属性。(10分)
(2)并定义该类的有参构造方法实现对属性的赋值。(5分)
(3)正确创建ArrayList集合对象。(5分)
(4)实例化5个U盘对象,并保存到ArrayList集合中。(5分)
(5)将ArrayList集合中的元素使用for循环输出。(10分)
(6)将所有u盘降价2元。(5分)
(7)正确输出结果。(10分)

 

package U;

public class Upan {

	private String type;// U盘品牌
	private double money;// 价格
	
	public Upan() {
	}

	public Upan(String type, double money) {
		this.type = type;
		this.money = money;
	}

	public String getType() {
		return type;
	}

	public void setType(String type) {
		this.type = type;
	}

	public double getMoney() {
		return money;
	}

	public void setMoney(double money) {
		this.money = money;
	}

	@Override
	public String toString() {
		return "Upan [type=" + type + ", money=" + money + "]";
	}

}
package U;

import java.util.ArrayList;
import java.util.List;

public class test {

	public static void main(String[] args) {
		Upan U1=new Upan("U盘品种1",80);
		Upan U2=new Upan("U盘品种2",70);
		Upan U3=new Upan("U盘品种3",60);
		Upan U4=new Upan("U盘品种4",50);
		Upan U5=new Upan("U盘品种5",40);
		
		List<Upan> pan=new ArrayList<Upan>();
		pan.add(U1);
		pan.add(U2);
		pan.add(U3);
		pan.add(U4);
		pan.add(U5);
		for(int i=0;i<pan.size();i++) {
			Upan p=pan.get(i);
			System.out.println(p.toString());
		}
		System.out.println("/****降价后*****/");
		for(int i=0;i<pan.size();i++) {
			Upan p=pan.get(i);
			p.setMoney(p.getMoney()-2);
			System.out.println(p.toString());
		}
	}

}

练习四:

功能描述:
使用ArrayList保存学生信息(学号,姓名,年龄,成绩),要求删除指定学生后遍历输出。

思路分析:
1、创建三个学生对象保存到集合
2、删除一个对象
3、遍历集合

 

package Student1;

public class Student {

	private String no;//学号
	private String name;//姓名
	private int age;//年龄
	private double score;//成绩
	public Student() {
		
	}
	public Student(String no, String name, int age, double score) {
		this.no = no;
		this.name = name;
		this.age = age;
		this.score = score;
	}
	public String getNo() {
		return no;
	}
	public void setNo(String no) {
		this.no = no;
	}
	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;
	}
	public double getScore() {
		return score;
	}
	public void setScore(double score) {
		this.score = score;
	}
	@Override
	public String toString() {
		return "Student [no=" + no + ", name=" + name + ", age=" + age + ", score=" + score + "]";
	}
	
	
}
package Student1;

import java.util.ArrayList;
import java.util.List;

public class test {

	public static void main(String[] args) {
		Student s1=new Student("18114","张三",22,80);
		Student s2=new Student("18115","李四",25,90);
		Student s3=new Student("18116","赵武",26,84);
		
		List<Student> stu=new ArrayList<Student>();
		stu.add(s1);
		stu.add(s2);
		stu.add(s3);
		System.out.println(stu.toString());
		
		//删除一个对象
		stu.remove(s2);
		for(int i=0;i<stu.size();i++) {
			System.out.println(stu.get(i));
		}
	}

}

练习五:

功能描述:
使用LinkedList保存人员信息:

思路分析:
1、创建四个人员名称
2、存入集合
3、输出显示

运行结果,如图:

 

代码如下:

package Person;

public class Person {

	private String name;//姓名

	public Person() {
		super();
		// TODO Auto-generated constructor stub
	}

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

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	@Override
	public String toString() {
		return this.name;
	}
	
}
package Person;

import java.util.LinkedList;

import org.junit.Test;

public class test {

	@Test
	public void test1() {
		Person p1=new Person("张三");
		Person p2=new Person("李四");
		Person p3=new Person("赵六");
		Person p4=new Person("王五");
		Person p5=new Person("憨豆");
		
		LinkedList<Person> per=new LinkedList<Person>();
		per.add(p1);
		per.add(p2);
		per.add(p3);
		per.add(p4);
		System.out.println(per.toString());
		
		per.add(p5);
		System.out.println(per.toString());
		
		per.remove(p1);
		System.out.println(per.toString());
	}
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值