题目1
题目需求:
实现使用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分)
----U盘类
public class UP {
private String brand;
private double price;
public UP(String brand, double price) {
super();
this.brand = brand;
this.price = price;
}
public UP() { }
public String getBrand() {
return brand;
}
public void setBrand(String brand) {
this.brand = brand;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
@Override
public String toString() {
return "UP [brand=" + brand + ", price=" + price + "]";
}
}
— 测试类
public void test4(){
ArrayList<UP> u = new ArrayList<UP>();
UP u1 = new UP("up1",165.00);
UP u2 = new UP("up2",265.00);
UP u3 = new UP("up3",365.00);
UP u4 = new UP("up4",465.00);
UP u5 = new UP("up5",465.00);
u.add(u1);
u.add(u2);
u.add(u3);
u.add(u4);
u.add(u5);
System.out.println("降价前");
for(UP p : u){
System.out.println(p);
p.setPrice(p.getPrice()-2.00);
}
System.out.println("降价后");
for(UP p : u){
System.out.println(p);
}
}
/*运行结果
降价前
UP [brand=up1, price=165.0]
UP [brand=up2, price=265.0]
UP [brand=up3, price=365.0]
UP [brand=up4, price=465.0]
UP [brand=up5, price=465.0]
降价后
UP [brand=up1, price=163.0]
UP [brand=up2, price=263.0]
UP [brand=up3, price=363.0]
UP [brand=up4, price=463.0]
UP [brand=up5, price=463.0]
*/
题目2
功能描述:
使用ArrayList保存学生信息(学号,姓名,年龄,成绩),要求删除指定学生后遍历输出。
思路分析:
1、创建三个学生对象保存到集合
2、删除一个对象
3、遍历集合
— 学生类
package Day0816am;
public class Student {
private String name;
private double score;
private String id;
private int age;
public Student() {
}
public Student(String name, double score, String id, int age) {
super();
this.name = name;
this.score = score;
this.id = id;
this.age = age;
}
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 String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
@Override
public String toString() {
return "Student [name=" + name + ", score=" + score + ", id=" + id + ", age=" + age + "]";
}
}
— 测试类
public void test3(){
ArrayList<Student> stu = new ArrayList<Student>();
stu.add(new Student("张三",98.0,"001213",18));
stu.add(new Student("李四",97.0,"565956",19));
stu.add(new Student("王五",88.0,"365459",65));
for(Student s : stu){
System.out.println(s);
}
//删除,通过索引删除
System.out.println("删除元素后");
stu.remove(1);
for(Student s : stu){
System.out.println(s);
}
/*
运行结果:
Student [name=张三, score=98.0, id=001213, age=18]
Student [name=李四, score=97.0, id=565956, age=19]
Student [name=王五, score=88.0, id=365459, age=65]
删除元素后
Student [name=张三, score=98.0, id=001213, age=18]
Student [name=王五, score=88.0, id=365459, age=65]
*/
}
题目3
要求:使用ArrayList保存学生信息(学号,姓名,年龄,成绩),要求删除指定学生后遍历输出。
(1) 正确创建集合类对象。(10分)
(2) 创建三个学生对象。(10分)
(3) 将学生对象保存到集合。(10分)
(4) 删除一个对象。(10分)
(5) 遍历集合。(5分)
(6) 正确输出结果。(5分)
—Stidnet类
package Day0816am;
public class Student {
private String name;
private double score;
private String id;
private int age;
public Student() {
}
public Student(String name, double score, String id, int age) {
super();
this.name = name;
this.score = score;
this.id = id;
this.age = age;
}
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 String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
@Override
public String toString() {
return "Student [name=" + name + ", score=" + score + ", id=" + id + ", age=" + age + "]";
}
}
—测试类
public void test3(){
ArrayList<Student> stu = new ArrayList<Student>();
stu.add(new Student("张三",98.0,"001213",18));
stu.add(new Student("李四",97.0,"565956",19));
stu.add(new Student("王五",88.0,"365459",65));
for(Student s : stu){
System.out.println(s);
}
//删除,通过索引删除
System.out.println("删除元素后");
stu.remove(1);
for(Student s : stu){
System.out.println(s);
}
/*
运行结果:
Student [name=张三, score=98.0, id=001213, age=18]
Student [name=李四, score=97.0, id=565956, age=19]
Student [name=王五, score=88.0, id=365459, age=65]
删除元素后
Student [name=张三, score=98.0, id=001213, age=18]
Student [name=王五, score=88.0, id=365459, age=65]
*/
}
题目4
完成以下题目要求:
(1) 定义一个狗类,狗有:名字,种类等属性。狗可以吃啃骨和跑方法。(10分)
(2) 再定义LinkedList集合类对象。(5分)
(3) 存储多条狗狗信息。(5分)
(4) 获取第一条和最后一条。(10分)
(5) 打印狗狗信息。(5分)
----狗类
package Day0816am;
public class Dog {
private String type;
private String name;
public void eat(){
System.out.println("啃骨头");
}
public void run(){
System.out.println("狗跑了");
}
public Dog(String type, String name) {
super();
this.type = type;
this.name = name;
}
public Dog() {
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public String toString() {
return "Dog [type=" + type + ", name=" + name + "]";
}
}
----测试类
@Test
public void test5(){
LinkedList<Dog> d = new LinkedList<Dog>();
Dog d1 = new Dog("泰迪1","狗狗1");
Dog d2 = new Dog("泰迪2","狗狗2");
Dog d3 = new Dog("泰迪3","狗狗3");
d.add(d1);
d.add(d2);
d.add(d3);
System.out.println(d.getFirst());
System.out.println(d.getLast());
System.out.println("全部打印");
for(Dog ds : d){
System.out.println(ds);
}
}
题目5
1、生成0-100中的10个随机数,将10个随机数放入集合中,判断随机数是否为偶数,将所有偶数放到一个新的集合中,并显示偶数个数。
package Day0816pm;
import java.util.ArrayList;
import java.util.Random;
public class First {
public static void main(String[] args) {
Random r = new Random();
ArrayList<Integer> n = new ArrayList<Integer>();
ArrayList<Integer> n1 = new ArrayList<Integer>();
for(int i=0;i<10;i++){
n.add(r.nextInt(101));
}
System.out.println("未分配之前的集合内容为");
for(Integer i : n){
System.out.println(i);
}
for(int i=0;i<n.size();i++){
int a = n.remove(i);
if(a % 2 == 0){
n1.add(a);
}
}
System.out.println("未分配之后的集合内容为");
for(Integer i : n1){
System.out.println(i);
}
}
}
题目6
1、定义一个学生类Student,包含三个属性姓名、年龄、性别,创建五个学生对象存入集合中。要求学生类封装代码,输出所有学生信息,并按照年龄从大到小排序。
-----学生类
package Day0816pm;
public class Student implements Comparable<Student>{
private String name;
private Integer age;
private String sex;
public Student() {
}
public Student(String name, Integer age, String sex) {
super();
this.name = name;
this.age = age;
this.sex = sex;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
@Override
public String toString() {
return "Student [name=" + name + ", age=" + age + ", sex=" + sex + "]";
}
@Override
public int compareTo(Student s) {
if(age > s.getAge()) {
return 1;
}else if(age == s.getAge()) {
return 0;
}
return -1;
}
}
----测试类
package Day0816pm;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
public class Second {
public static void main(String[] args) {
Student s1 = new Student("张三1",15,"女");
Student s2 = new Student("张三2",95,"女");
Student s3 = new Student("张三3",85,"男");
Student s4 = new Student("张三4",45,"女");
Student s5 = new Student("张三5",55,"男");
List<Student> stus = new ArrayList<Student>();
stus.add(s1);
stus.add(s2);
stus.add(s3);
stus.add(s4);
stus.add(s5);
Collections.sort(stus);
Collections.reverse(stus);
for(Student stu : stus){
System.out.println(stu);
}
}
}
题目7
1、创建一个Computer类,属性为编号(即计算机的唯一标识)CPU型号,主板型号,硬盘大小,内存大小,显卡型号,价格;
使用集合存储计算机对象
为集合添加3个元素。
用户输入“编号”获取对应的Computer信息,并且输出具体的参数。
----Computer类
package Day0816pm;
public class Computer {
private String id;//编号(即计算机的唯一标识)
private String CPUtype;//CPU型号,;
private String mainType;//主板型号,
private double HDsize;//硬盘大小,
private double RAM;//内存大小,
private String xkType;//显卡型号
private double price;//,价格
public Computer() {}
public Computer(String id, String cPUtype, String mainType, double hDsize, double rAM, String xkType,
double price) {
super();
this.id = id;
CPUtype = cPUtype;
this.mainType = mainType;
HDsize = hDsize;
RAM = rAM;
this.xkType = xkType;
this.price = price;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getCPUtype() {
return CPUtype;
}
public void setCPUtype(String cPUtype) {
CPUtype = cPUtype;
}
public String getMainType() {
return mainType;
}
public void setMainType(String mainType) {
this.mainType = mainType;
}
public double getHDsize() {
return HDsize;
}
public void setHDsize(double hDsize) {
HDsize = hDsize;
}
public double getRAM() {
return RAM;
}
public void setRAM(double rAM) {
RAM = rAM;
}
public String getXkType() {
return xkType;
}
public void setXkType(String xkType) {
this.xkType = xkType;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
@Override
public String toString() {
return "Computer [id=" + id + ", CPUtype=" + CPUtype + ", mainType=" + mainType + ", HDsize=" + HDsize
+ ", RAM=" + RAM + ", xkType=" + xkType + ", price=" + price + "]";
}
}
— 测试类
package Day0816pm;
import java.util.ArrayList;
import java.util.Scanner;
public class Three {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
ArrayList<Computer> cm = new ArrayList<Computer>();
Computer cm1 = new Computer("123456","bf2001","bt2002",100.00,20.00,"xk2001",2000.0);
Computer cm2 = new Computer("123457","bf2002","bt2003",110.00,20.00,"xk2001",2000.0);
Computer cm3 = new Computer("123458","bf2003","bt2004",120.00,20.00,"xk2001",2000.0);
cm.add(cm1);
cm.add(cm2);
cm.add(cm3);
System.out.println("所有型号:");
for(Computer c : cm){
System.out.println(c);
}
String type = null;
while(true){
for(int i=0;i<cm.size();i++){
System.out.println("请输入要查询的型号:");
type = sc.next();
if(type.equals(cm.get(i).getId())){
System.out.println(cm.get(i));
}else{
System.out.println("输入错误,请重新输入!");
}
}
}
}
}
题目8
1、生成0-9的20个随机数,将20个随机数放入集合中,手动输入0-9的个位数,将集合中的出现的这个个位数放到集合最前面,优先输出。
package Day0816pm;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Random;
import java.util.Scanner;
public class Four {
public static void main(String[] args) {
Random r = new Random();
Scanner sc = new Scanner(System.in);
LinkedList<Integer> j = new LinkedList<Integer>();
for(int i=0;i<20;i++){
int n = r.nextInt(10);
j.add(n);
}
System.out.println("没有改变前的排序:");
System.out.println(j);
System.out.println("输入想要放到最前面的数字:");
Integer a = sc.nextInt();
for(int i=0;i<j.size();i++){
if(a == j.get(i)){
j.remove(i);
j.addFirst(a);
}
}
System.out.println("改变后的排序:");
System.out.println(j);
}
}
题目9
1、生成0-100中的10个随机数,将10个随机数放入集合中,将整个集合倒置,放到新的集合中输出,即原集合第一位放到新集合最后一位。
注:生成随机数方法
Random random = new Random();
int num = random.nextInt(100);生成随机数0-100
package Day0816pm;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Random;
public class Five {
public static void main(String[] args) {
Random r = new Random();
ArrayList<Integer> n = new ArrayList<Integer>();
ArrayList<Integer> n1 = new ArrayList<Integer>();
for(int i=0;i<10;i++){
n.add(r.nextInt(101));
}
System.out.println("未分配之前的集合内容为");
for(Integer i : n){
System.out.println(i);
}
Collections.reverse(n);
System.out.println("之后的集合内容为");
for(Integer i : n){
System.out.println(i);
}
}
}
成0-100中的10个随机数,将10个随机数放入集合中,将整个集合倒置,放到新的集合中输出,即原集合第一位放到新集合最后一位。
注:生成随机数方法
Random random = new Random();
int num = random.nextInt(100);生成随机数0-100
package Day0816pm;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Random;
public class Five {
public static void main(String[] args) {
Random r = new Random();
ArrayList<Integer> n = new ArrayList<Integer>();
ArrayList<Integer> n1 = new ArrayList<Integer>();
for(int i=0;i<10;i++){
n.add(r.nextInt(101));
}
System.out.println("未分配之前的集合内容为");
for(Integer i : n){
System.out.println(i);
}
Collections.reverse(n);
System.out.println("之后的集合内容为");
for(Integer i : n){
System.out.println(i);
}
}
}