将一个自定义未知容量的集合逆序输出
例如 集合{1,2,3} 输出 为 3 ,2 ,1。
package XIAOBU;
import java.util.ArrayList;
//1. 将一个自定义未知容量的集合逆序输出
// 例如 集合{1,2,3} 输出 为 3 ,2 ,1。
public class Q1 {
public static void main(String[] args) {
ArrayList<Integer> list=new ArrayList<>();
//ArrayList默认基础容量为10 后续添加 容量按1.5倍扩大
list.add(1);
list.add(2);
list.add(3);
for (int i = list.size() - 1; i >= 0; i--) {
System.out.println(list.get(i));//递减for循环 遍历输出
}
}
}
结果
解题思路:ArraysList 底层为数组 只需要用for循环 反向遍历下标得值 即可
定义一个10*10的二维数组 用随机数(范围0~99) 填充到二维数组中
遍历二维数组 将最大值找出来 二维数组类型为int
package XIAOBU;
//2. 定义一个10*10的二维数组 用随机数(范围0~99) 填充到二维数组中
// 遍历二维数组 将最大值找出来 二维数组类型为int
public class Q2 {
public static void main(String[] args) {
int [][] a=new int[10][10];//10*10容量为100的二维数组
//因为要填满数组 所以需要执行100次循环,又因为是二维数组所以需要嵌套循环
for (int i = 0; i < 10; i++) {
for (int j = 0; j < 10; j++) {
// Math.random()是令系统随机选取大于等于 0.0 且小于 1.0 的伪随机 double 值
//公式:Math.random()*(n-m)+m,生成大于等于m小于n的随机数;
int x = (int) (Math.random() * 100); //生成0~99的随机数
System.out.println(x);
a[i][j]=x;//将随机数添加到数组中
}
}
//填充完数组 再次遍历二维数组获取最大值
int max=0;//因为要得到他的最大值 所以要定义一个标记 对每一次生成的随机数进行一次比较 从而获取最大值
for (int i = 0; i < 10; i++) {
for (int j = 0; j < 10; j++) {
int z=a[i][j];
if (z>max){//如果z大于max 则将z的值赋值给max 不大于则不进行赋值 这样遍历完全部 最后得到的就是最大值了
max=z;
}
}
}
System.out.println("最大值为"+max);
}
}
结果
解题思路:二维数组就像一个二维坐标系 有行有列 需要嵌套for循环遍历操作二维数组。 外层for 控制 行 ,内层 for 控制 列。
随机数使用Math.random(): 公式:Math.random()*(n-m)+m,生成大于等于m小于n的随机数;
得到最大值 需要用一个对比对象max 来和每一个值进行比较, 大于 则 取代。
定义一个信息管理系统 (Information) 里面存放在人们的各种信息
// 要求:个人信息包括 姓名 年龄 性别 职业(学生,社会人士,残障)。
// 每一项信息都可以根据名字单独查询(可能需要 map 集合)。
// 系统中有一个方法可以统计 所有人的平均年龄的方法
Information类
public class Information {
Map<String,people> map=new HashMap<>();
public void getAVG_age(){// 获取平均年龄
// 迭代器遍历 map 集合 有很多种方法 这里只写一种
Iterator<Map.Entry<String, people>> iterator=map.entrySet().iterator();
int allAge=0;
while (iterator.hasNext()){
Map.Entry<String, people> m= iterator.next();
allAge=allAge+m.getValue().getAge();
}
//
System.out.println(allAge/map.size());
}
public void addPeople(people p){// 添加对象(人)
map.put(p.getName(),p);//添加到集合种
}
public void getPeople(String name){
people people=map.get(name);//根据名字获取 个人资料
if (people==null){
System.out.println("查无此人");
}else {
System.out.println(people);
}
}
}
people类
class people{
private int age;
private String name;
private String role;
private String sex;
public people(int age, String name, String role, String sex) {
this.age = age;
this.name = name;
this.role = role;
this.sex = sex;
}
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 String getRole() {
return role;
}
public void setRole(String role) {
this.role = role;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
@Override
public String toString() {
return "people{" +
"age=" + age +
", name='" + name + '\'' +
", role='" + role + '\'' +
", sex='" + sex + '\'' +
'}';
}
}
测试类
class test{
public static void main(String[] args) {
Information information=new Information();
people p1=new people(20,"小布","学生","男");
people p2=new people(20,"李娇娇","学生","女");
// 定义了两个人 并将他们添加到信息管理系统中去
information.addPeople(p1);
information.addPeople(p2);
information.getAVG_age();//获取平均年龄
information.getPeople("小布");//根据名字查询
}
}
结果
解题思路:需要建立一个people对像 来存储信息 从而进行系统操作
4. 用字节流写几个字到txt文件中 再用字符流读取到控制台中。
package XIAOBU;
import java.io.*;
//4. 用字节流写几个字到txt文件中 再用字符流读取到控制台中。
public class Q4 {
public static void main(String[] args) throws IOException {
File file=new File("C:\\Users\\我\\IdeaProjects\\untitled\\src\\src\\XIAOBU\\test04.txt");
FileOutputStream fileOutputStream =new FileOutputStream(file);//字节流
String x="永远滴神";//写入内容
byte []bytes=x.getBytes();//将x转换成字节数组
fileOutputStream.write(bytes);
fileOutputStream.close();
//↑ 字节写入操作
//↓ 字符读出操作
FileReader fileReader=new FileReader(file);
int len;
int f=0;
f=fileReader.read();
while (f!=-1){
System.out.print((char)f);
f=fileReader.read();
}
fileReader.close();
}
}
结果
5. 用字符流写几个字到txt文件中 再用字节流读取到控制台中。
package XIAOBU;
import java.io.*;
// 5. 用字符流写几个字到txt文件中 再用字节流读取到控制台中。
public class Q5 {
public static void main(String[] args) throws IOException {
File file=new File("C:\\Users\\我\\IdeaProjects\\untitled\\src\\src\\XIAOBU\\test05.txt");
FileWriter fileWriter=new FileWriter(file);
BufferedWriter bufferedWriter=new BufferedWriter(fileWriter);
bufferedWriter.write("YYDS");
bufferedWriter.close();
fileWriter.close();
//↑字符流写入操作
//↓字节流读取操作
FileInputStream in=new FileInputStream(file);
byte[] bytes=new byte[1024];//定义缓存
int len=0;
while ((len=in.read(bytes))!=-1){
System.out.println(new String(bytes));
}
}
}