You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
banban/src/main/java/com/util/test.java

139 lines
5.0 KiB

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

package com.util;
import java.io.Console;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.Random;
import com.alibaba.fastjson.JSONObject;
public class test {
public static void main(String[] args) {
// 以下代码被注释掉了,原本用于测试 Date 和 java.sql.Date 的转换
/*
Date date = new Date();
java.sql.Date zdate = new java.sql.Date(date.getYear(), date.getMonth(), date.getDay());
System.out.println(date);
System.out.println(zdate);
*/
// 生成20个5到9之间的随机浮点数并格式化为一位小数后输出
/*
for(int i = 0; i < 20; i++) {
float random = 5 + (new Random().nextFloat() * 4);
DecimalFormat fnum = new DecimalFormat("##0.0");
String score = fnum.format(random);
System.out.println("score:" + score);
}
*/
// 构建一个包含两个影院信息的 JSON 对象每个影院有2个影厅
/*
JSONObject obj = new JSONObject();
JSONObject hallobj = new JSONObject();
for(int i = 0; i < 2; i++) {
ArrayList<String> cinemalist = new ArrayList<String>();
for(int j = 0; j < 2; j++) {
cinemalist.add(String.valueOf(j + 1) + "号厅");
}
hallobj.put("影院" + String.valueOf(i), cinemalist);
}
obj.put("cinema", hallobj);
System.out.println(obj);
// 解析一级 JSON 对象
JSONObject obj2 = obj.getJSONObject("cinema");
System.out.println(obj2);
// 解析二级 JSON 对象
ArrayList<String> halllist = (ArrayList<String>)obj2.get("影院0");
System.out.println(halllist.get(0));
*/
// 创建一个整数列表并添加5个元素然后打印最后一个元素
/*
ArrayList<Integer> arr = new ArrayList<Integer>();
for(int i = 0; i < 5; i++) {
arr.add(i);
}
System.out.println(arr.get(4));
*/
// 格式化当前日期为 YYYYMMdd 格式的字符串
/*
Date date = new Date();
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMdd"); // 注意:使用小写的 'y' 表示年份
String str = dateFormat.format(date);
System.out.println(str);
*/
// 将座位字符串数组转换为标准化的索引形式例如“3排4座” -> "0304"
/*
String[] seats = {"3排4座", "10排1座", "5排12座", "11排12座"};
ArrayList<String> arr = new ArrayList<String>();
for (String seat : seats) {
String index = "";
switch (seat.length()) {
case 4:
index = "0" + seat.replaceAll("排", "0").replaceAll("座", "");
break;
case 5:
if (Character.isDigit(seat.charAt(2))) {
index = "0" + seat.replaceAll("排", "").replaceAll("座", "");
} else {
index = seat.replaceAll("排", "0").replaceAll("座", "");
}
break;
case 6:
index = seat.replaceAll("排", "").replaceAll("座", "");
break;
}
arr.add(index);
}
System.out.println(arr);
*/
// 将价格转换为万元单位并格式化为四位小数
/*
int price = 29;
float box = (float) price / 10000;
System.out.println(box);
*/
// 使用 DecimalFormat 格式化数字为四位小数
/*
float random = (float) Math.random(); // 假设这里有一个随机数
DecimalFormat fnum = new DecimalFormat("##0.0000");
String score = fnum.format(random);
System.out.println(score);
*/
// 创建一个整数列表,将数组中的非零元素添加到列表中,并记录零元素的位置
ArrayList<Integer> arr = new ArrayList<>();
ArrayList<Integer> indexz = new ArrayList<>();
int num[] = {1, 2, 3, 0, 5, 0, 6};
for (int i = 0; i < num.length; i++) {
arr.add(num[i]);
}
// 找出所有值为0的元素的索引位置
for (int z = 0; z < arr.size(); z++) {
if (arr.get(z) == 0) {
indexz.add(z);
}
}
System.out.println(arr); // 打印原始列表
System.out.println(indexz); // 打印零元素的索引
// 移除所有值为0的元素注意移除时需要调整索引因为每次移除后列表大小会变化
for (int y = 0; y < indexz.size(); y++) {
int index = indexz.get(y) - y; // 调整索引以适应列表大小的变化
System.out.println(index);
arr.remove((int)index); // 移除对应索引的元素
}
System.out.println(arr); // 打印移除后的列表
}
}