Java--集合--经典七道综合练习题

news2024/9/25 21:23:26

文章目录

一、集合的遍历方式

二、添加数字并遍历

三、添加学生对象并遍历

四、添加学生对象并遍历

五、添加用户对象并判断是否存在

六、添加手机对象并返回要求的数据

七、创建学生管理系统(*****)

一、集合的遍历方式

需求:定义一个集合,添加字符串,并进行遍历。

遍历格式参照:[元素1,元素2,元素3]。

import java.util.ArrayList;

public class ArrayListTest1 {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		
		//1.创建一个集合
		ArrayList<String> list=new ArrayList<>();
		
		//2.添加元素
		list.add("aaa");
		list.add("bbb");
		list.add("ccc");
		list.add("ddd");
		
		//3.遍历集合
		System.out.print("[");
		for(int i=0;i<list.size();i++) {
			if(i == list.size()-1) {
				System.out.print(list.get(i));
			}else {
				System.out.print(list.get(i)+", ");
			}
		}
		System.out.print("]");

	}

}

二、添加数字并遍历

需求:定义一个集合,添加数字,并进行遍历。

遍历格式参照:[元素1,元素2,元素3]。

import java.util.ArrayList;

public class ArrayListTest2 {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		
		//1.创建一个集合
		ArrayList<Integer> list=new ArrayList<>();
		
		//2.添加元素
		list.add(1);
		list.add(2);
		list.add(3);
		list.add(4);
		
		//3.遍历集合
		System.out.print("[");
		for(int i=0;i<list.size();i++) {
			if(i == list.size()-1) {
				System.out.print(list.get(i));
			}else {
				System.out.print(list.get(i)+", ");
			}
		}
		System.out.print("]");

	}

}

三、添加学生对象并遍历

需求:定义一个集合,添加一些学生对象,并进行遍历

           学生类的属性为:姓名,年龄。

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

	

}
import java.util.ArrayList;

public class ArrayListTest3 {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		
		//1.创建一个集合
		ArrayList<Student_Test3> list=new ArrayList<>();
		
		//2.创建学生对象
		Student_Test3 st1=new Student_Test3("张三",23);
		Student_Test3 st2=new Student_Test3("李四",24);
		Student_Test3 st3=new Student_Test3("王五",25);
		
		//3.将学生对象添加到集合里
		list.add(st1);
		list.add(st2);
		list.add(st3);
		
		//4.遍历集合
		for(int i=0;i<list.size();i++) {
			Student_Test3 stu=list.get(i);
			System.out.println(stu.getName()+", "+stu.getAge());
		}
		

	}

}

 

四、添加学生对象并遍历

需求:定义一个集合,添加一些学生对象,并进行遍历

           学生类的属性为:姓名,年龄。

要求:对象的数据来自键盘录入

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

	

}
import java.util.ArrayList;
import java.util.Scanner;

public class ArrayListTest4 {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		
		//1.创建一个集合
		ArrayList<Student_Test3> list=new ArrayList<>();
		
		//2.键盘录入学生信息
		Scanner sc=new Scanner(System.in);
		for(int i=0;i<3;i++) {
			Student_Test3 s=new Student_Test3();
			System.out.println("请输入学生的姓名:");
			String name=sc.next();
			System.out.println("请输入学生的年龄:");
			int age=sc.nextInt();
			
			//把name 和age 赋值给学生对象
			s.setName(name);
			s.setAge(age);
			
			//把学生信息添加到集合中
			list.add(s);
		}
		
		//3.遍历集合
		for(int i=0;i<list.size();i++) {
			Student_Test3 stu=list.get(i);
			System.out.println(stu.getName()+", "+stu.getAge());
		}

	}

}

五、添加用户对象并判断是否存在

需求:

1.main方法中定义一个集合,存入三个用户对象。

  用户属性为:id,username,password

2.要求:定义一个方法,根据id查找对应的用户信息。

  如果存在,返回true

  如果不存在,返回false

public class User {
	private String id;
	private String username;
	private String password;
	
	public User() {
		
	}
	
	public User(String id,String username,String password) {
		this.id=id;
		this.username=username;
		this.password=password;
	}
	
	public void setId(String id) {
		this.id=id;
	}
	
	public String getId() {
		return id;
	}
	
	public void setUsername(String username) {
		this.username=username;
	}
	
	public String getUsername() {
		return username;
	}
	
	public void setPassword(String password) {
		this.password=password;
	}
	
	public String getPassword() {
		return password;
	}


}
import java.util.ArrayList;

public class ArrayListTest5 {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		
		//1.创建一个集合
		ArrayList<User> list=new ArrayList<>();
		
		//2.创建用户对象
		User u1=new User("001","zhangsan","1234501");
		User u2=new User("002","lisi","1234502");
		User u3=new User("003","wangwu","1234503");
		
		
		//3.将用户对象添加到集合中
		list.add(u1);
		list.add(u2);
		list.add(u3);
		
		
		//
		boolean flag=check(list,"005");
		if(flag) {
			System.out.println("存在");
		}else {
			System.out.println("不存在");
		}
		

	}
	public static boolean check(ArrayList<User> list,String id) {
		for(int i=0;i<list.size();i++) {
			if(list.get(i).getId().equals(id)) {
				return true;
			}
		}
		
		return false;
	}

}

六、添加手机对象并返回要求的数据

需求:

定义JavaBean类:Phone

Phone的属性:品牌、价格。

main方法中定义一个集合,存入三个手机对象。

分别为:小米,1000。苹果,8000。锤子,2999。

定义一个方法,将价格低于3000的手机信息返回

public class Phone {
	private String brand;
	private int price;
	
	public Phone() {
		
	}
	
	public Phone(String brand,int price) {
		this.brand=brand;
		this.price=price;
	}
	
	public void setBrand(String brand) {
		this.brand=brand;
	}
	
	public String getBrand() {
		return brand;
	}
	
	public void setPrice(int price) {
		this.price=price;
	}
	
	public int getPrice() {
		return price;
	}
	

}
import java.util.ArrayList;

public class ArrayListTest6 {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		
		//1.创建一个集合
		ArrayList<Phone> list=new ArrayList<>();
		
		//2.创建用户对象
		Phone p1=new Phone("小米",1000);
		Phone p2=new Phone("苹果",8000);
		Phone p3=new Phone("锤子",2999);
		
		//3.添加到集合中
		list.add(p1);
		list.add(p2);
		list.add(p3);
		
		//4.调用方法
		ArrayList<Phone> phoneInfoList=getPhoneInfo(list);
		
		//5.遍历集合
		for(int i=0;i<phoneInfoList.size();i++) {
			Phone phone=phoneInfoList.get(i);
			System.out.println(phone.getBrand()+", "+phone.getPrice());
		}

	}
	public static ArrayList<Phone> getPhoneInfo(ArrayList<Phone> list) {
		ArrayList<Phone> resultList=new ArrayList<>();
		
		for(int i=0;i<list.size();i++) {
			Phone p=list.get(i);
			int price=p.getPrice();
			if(price<3000) {
				resultList.add(p);
			}
		}
		
		return resultList;
	}

}

七、创建学生管理系统

需求:

 

 

 

public class Student {
	private String id;
	private String name;
	private int age;
	private String address;
	
	public Student() {
		
	}
	
	public Student(String id,String name,int age,String address) {
		this.id=id;
		this.name=name;
		this.age=age;
		this.address=address;
	}
	
	public void setName(String name) {
		this.name=name;
	}
	
	public String getName() {
		return name;
	}
	
	public void setAge(int age) {
		this.age=age;
	}
	
	public int getAge() {
		return age;
	}
	
	public void setId(String id) {
		this.id=id;
	}
	
	public String getId() {
		return id;
	}
	
	public void setAddress(String address) {
		this.address=address;
	}
	
	public String getAddress() {
		return address;
	}


}
import java.util.ArrayList;
import java.util.Scanner;

public class Test_综合练习 {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		
		ArrayList<Student> list=new ArrayList<>();
		
		loop:while(true) {
			System.out.println("-------------欢迎来到黑马学生管理系统--------------");
			System.out.println("1:添加学生");
			System.out.println("2:删除学生");
			System.out.println("3:修改学生");
			System.out.println("4:查询学生");
			System.out.println("5:退出");
			
			
			Scanner sc=new Scanner(System.in);
			System.out.println("请输入您的选择:");
			String choose=sc.next();
			
			switch(choose) {
			case "1":addStudent(list);break;
			case "2":deleteStudent(list);break;
			case "3":updateStudent(list);break;
			case "4":queryStudent(list);break;
			case "5":System.out.println("5:退出");break loop;
			default:System.out.println("没有这个选项");
		}
		
			
		}

	}
	public static void addStudent(ArrayList<Student> list) {
		Scanner sc=new Scanner(System.in);
		//创建学生对象
		Student s=new Student();
		
		while(true) {
			System.out.println("请输入学生的id:");
			String id=sc.next();
			boolean flag=contains(list,id);
			if(flag) {
				//表示id已经存在,需要重新输入
				System.out.println("id已经存在,请重新输入");
			}else {
				s.setId(id);
				break;
			}
		}
		
		System.out.println("请输入学生的姓名:");
		String name=sc.next();
		s.setName(name);
		
		System.out.println("请输入学生的年龄:");
		int age=sc.nextInt();
		s.setAge(age);
		
		System.out.println("请输入学生的家庭住址:");
		String address=sc.next();
		s.setAddress(address);
		
		//将学生对象添加到数组当中
		list.add(s);
		
		//提示一下用户信息添加成功
		System.out.println("用户信息添加成功!");
		
		
	}
	
	public static void deleteStudent(ArrayList<Student> list) {
		Scanner sc=new Scanner(System.in);
		System.out.println("请输入要删除的学生id:");
		String id=sc.next();
		//查询id在集合中的索引
		int index=getIndex(list,id);
		if(index>=0) {
			list.remove(index);
			System.out.println("id为:"+id+"的学生删除成功");
		}else {
			System.out.println("id不存在,删除失败");
		}
		
		
	}
	
	public static void updateStudent(ArrayList<Student> list) {
		Scanner sc=new Scanner(System.in);
		System.out.println("请输入要修改学生的id:");
		String id=sc.next();
		
		int index=getIndex(list,id);
		if(index==-1) {
			System.out.println("要修改的id"+id+"不存在,请重新输入");
			return;
		}
		
		Student stu=list.get(index);
		
		//输入其他的新型修改
		System.out.println("请输入要修改的姓名:");
		String newName=sc.next();
		stu.setName(newName);
		
		System.out.println("请输入要修改的年龄:");
		int newAge=sc.nextInt();
		stu.setAge(newAge);
		
		System.out.println("请输入要修改的家庭住址::");
		String newAddress=sc.next();
		stu.setAddress(newAddress);
		
		System.out.println("学生信息修改成功!");
		
	}
	
	public static void queryStudent(ArrayList<Student> list) {
		if(list.size()==0) {
			System.out.println("当前无学生信息,请添加后在查询");
			//结束方法
			return; 
		}
		
		System.out.println("id\t\t姓名\t\t年龄\t"+"家庭住址");
		for(int i=0;i<list.size();i++) {
			Student stu=list.get(i);
			System.out.println(stu.getId()+"\t"+stu.getName()+"\t\t"+stu.getAge()+"\t"+stu.getAddress());
		}
	}
	
	public static boolean contains(ArrayList<Student> list,String id) {
		for(int i=0;i<list.size();i++) {
			Student stu=list.get(i);
			if(stu.getId().equals(id)) {
				return true;
			}
		}
		return false;
	}
	
	public static int getIndex(ArrayList<Student> list,String id) {
		
		for(int i=0;i<list.size();i++) {
			Student stu=list.get(i);
			if(stu.getId().equals(id)) {
				return i;
			}
		}
		
		return-1;
	}

}

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/151134.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

深耕5G云专网,阿里云祝顺民入选“2022年度5G创新人物”

2022 年&#xff0c;5G 商用发展成效显著&#xff0c;正在加速产业数字化&#xff0c;全面赋能经济社会发展。运营商 5G 投资超过 4000 亿元&#xff0c;共带动 8.6 万亿元的经济产出&#xff0c;而这一切离不开奋战在 ICT 行业的 5G 应用推动者。 日前&#xff0c;由中国工信…

分享104个PHP源码,总有一款适合您

PHP源码 分享104个PHP源码&#xff0c;总有一款适合您 链接&#xff1a;https://pan.baidu.com/s/1MnmNb3vsofBnQ4kKoMlSBw?pwdkl4o 提取码&#xff1a;kl4o 下面是文件的名字&#xff0c;我放了一些图片&#xff0c;文章里不是所有的图主要是放不下...&#xff0c;大家下载…

Python学生信息管理系统源码,学生教师端分离,支持数据的增删查改、数据分析与统计 基于Tkinter带图形界面

介绍 Python学生信息管理系统。学生教师端分离&#xff0c;支持数据的增删查改、数据分析与统计。 下载地址&#xff1a;Python学生信息管理系统源码 软件架构 Python3.9版本 Python-Tkinter库开发的图形界面 Python-Pandas库数据处理 Python-matplotlib库数据分析与展示 P…

C语言经典100例(006,007)

题目&#xff1a;用*号输出字母C的图案。 程序分析&#xff1a;可先用*号在纸上写出字母C&#xff0c;再分行输出。 程序源代码&#xff1a; #include "stdio.h" int main() {printf("用 * 号输出字母 C!\n");printf(" ****\n");printf("…

CSS知识点精学7-小兔鲜项目实现案例

一.网页和网站的关系 包含关系 网页和网站的关系就是包含关系&#xff0c;网站包含了很多的网页&#xff0c;网页不能单独存在于网络中。 网站是一个整体&#xff0c;网页是一个个体&#xff0c;一个网站是由很多网页构建而成。就像进入百度网站&#xff0c;里面还有其他许多…

AI作画怎么弄?超详细ai绘画教程在这里

AI作画怎么弄&#xff1f;如何实现将照片生成ai漫画图&#xff0c;如何通过关键词描述生成好看的壁纸、背景&#xff1f;最全最详细教程来了&#xff0c;一分钟学会&#xff01; 一、将照片生成二次元 我们先打开数画ai绘画软件&#xff0c;在首页这里&#xff0c;点击“相册”…

【Git】Git常用命令

3、Git 常用命令 命令名称作用git config --global user.name 用户名设置用户签名git config --global user.email 邮箱设置用户签名git init初始化本地库git status查看本地库状态git add 文件名添加到暂存区git commit -m “日志信息” 文件名提交到本地库git reflog查看历史…

手写RPC框架06-基于线程和队列提升框架并发处理能力

源代码地址&#xff1a;https://github.com/lhj502819/IRpc/tree/v7 系列文章&#xff1a; 注册中心模块实现路由模块实现序列化模块实现过滤器模块实现自定义SPI机制增加框架的扩展性的设计与实现基于线程和队列提升框架并发处理能力 Server端 现有的问题 目前我们的RPC框…

酷早报:1月9日全球Web3加密行业重大资讯大汇总

2023年1月9日 星期一 【数据指标】 加密货币总市值&#xff1a;$0.84万亿 BTC市值占比&#xff1a;39.14% 恐慌贪婪指数&#xff1a;25 极度恐慌【今日快讯】 1、【政讯】 1.1、美债关键收益率曲线倒挂幅度创纪录以来新高 1.2.1、美联储博斯蒂克&#xff1a;倾向于将利率升至5%…

2020年MathorCup高校数学建模挑战赛—大数据竞赛A题移动通信基站流量预测求解全过程文档及程序

2020年MathorCup高校数学建模挑战赛—大数据竞赛 A题 移动通信基站流量预测 原题再现&#xff1a; 随着移动通信技术的发展&#xff0c;4G、5G 给人们带来了极大便利。移动互联网的飞速发展&#xff0c;使得移动流量呈现爆炸式增长&#xff0c;从而基站的流量负荷问题变得越来…

代码随想录第55天|● 392.判断子序列 ● 115.不同的子序列

392.判断子序列 dp[i][j] 表示以下标i-1为结尾的字符串s&#xff0c;和以下标j-1为结尾的字符串t&#xff0c;相同子序列的长度为dp[i][j]。 if (s[i - 1] t[j - 1])&#xff0c;那么dp[i][j] dp[i - 1][j - 1] 1;&#xff0c;因为找到了一个相同的字符&#xff0c;相同子…

当没有成熟案例可参考时,企业该如何实现数字化转型?

对于企业来说&#xff0c;数字化转型过程中&#xff0c;参考成熟的案例是可以提高成功率的。但是在现实中&#xff0c;很多企业由于行业、领先地位、技术保密性等原因&#xff0c;导致没有或者找不到可参考的数字化转型案例为自身提供经验。那么这种情况下该如何做呢&#xff0…

Java中日期和时间的类

文章目录JDK8之前日期和时间的APISystem类中的Date类中的java.util.Date类中的二个构造器二个方法java.sql.Date类中的实例化将java.sql.Date类对象转化为java.util.Date类的对象将java.util.Date类对象转化为java.sql.Date类的对象每日一考JDK8之前日期和时间的API System类中…

你对Bug了解多少?如何“正确的”向开发人员提出Bug?

目录 一、Bug的级别 二、Bug的生命周期 三、如何向开发人员提出Bug&#xff08;如何创建Bug&#xff09;? 四、跟开发产生争执怎么办&#xff1f;&#xff08;面试高频&#xff09; 一、Bug的级别 为什么Bug也要存在级别&#xff1f;不同的Bug等级&#xff0c;惩罚机制不一…

环形缓冲区

文章目录一. 什么是环形缓冲区&#xff1f;二、实现环形缓冲区&#xff1a;三、环形缓冲区示例代码&#xff1a;总结一. 什么是环形缓冲区&#xff1f; 环形缓冲区 是一段 先进先出 的循环缓冲区&#xff0c;有一定的大小&#xff0c;我们可以把它抽象理解为一块环形的内存。 …

快速掌握web服务器相关知识

目录 1.web服务器 2.HTTP的状态码 3.web实验 4.算法介绍 1.web服务器 web服务器指网站服务器&#xff0c;是指驻留与因特网上某种类型计算机的程序&#xff0c;可以向浏览器等WEB客户端提供文档&#xff0c;也可以放置网站文件&#xff0c;让全世界浏览&#xff1b;可以放置…

关于batchnormlization理解

论文一般是这两张典型图片引用wz博客辅助理解上图展示了一个batch size为2&#xff08;两张图片&#xff09;的Batch Normalization的计算过程&#xff0c;假设feature1、feature2分别是由image1、image2经过一系列卷积池化后得到的特征矩阵&#xff0c;feature的channel为2&am…

TCP三次握手和四次挥手

三次握手 先ping域名为www.baidu.com&#xff0c;便于DNS解析。ping走的协议就包括DNS、ARP和ICMP。 接着使用Wireshark去抓包&#xff0c;抓包这里导航栏直接过滤ip就可以了&#xff0c;输入ip.host 183.232.231.174 接着直接在浏览器输入百度域名www.baidu.com访问请求&am…

TikTok新规:严禁录播盗播,保护原创内容

让我们一起来看看今日都有哪些新鲜事吧&#xff01;01 2023年&#xff0c;TikTok将在社交买家渗透率和用户使用时间上面成为美国第一 eMarketer最新预测显示&#xff0c;TikTok的社交买家渗透率和用户使用时间正在迅速攀升&#xff0c;预计将在2023年分别超过Facobook和Youtub…

全景剖析阿里云容器网络数据链路(一):Flannel

作者&#xff1a;余凯 本系列文章由余凯执笔创作&#xff0c;联合作者&#xff1a;阿里云云原生应用平台 谢石 对本文亦有贡献 前言 近几年&#xff0c;企业基础设施云原生化的趋势越来越强烈&#xff0c;从最开始的 IaaS 化到现在的微服务化&#xff0c;客户的颗粒度精细化…