内部类
匿名内部类
API
public static void main(String[] args) {
int[] ar = {30, 55, 2, 656, 80};
System.out.println(style(ar));
//第x次循环
for (int j = 0; j < ar.length - 1; j++) {
for (int i = 0; i < ar.length - 1-j; i++) {
if (ar[i] > ar[i + 1]) {
int temp = ar[i + 1];
ar[i + 1] = ar[i];
ar[i] = temp;
}
}
System.out.println(style(ar));
}
}
public static String style(int[] ar) {
StringBuilder sb = new StringBuilder();
sb.append("[");
for (int i = 0; i < ar.length; i++) {
if (i == ar.length - 1) {
sb.append(ar[i]);
} else {
sb.append(ar[i]).append(",");
}
}
sb.append("]");
String s = sb.toString();
return s;
}
Arrays
基本类类型包装类
Integer类
基本类型包装类
案例:字符串中数据排序
public static void main(String[] args) {
String s="22 35 33 2 64 16";
//用空格分隔长度
String [] tr=s.split(" ");
// System.out.println(tr.length);
//String类型转换成int类型
int [] n=new int[tr.length];
for(int i=0;i<n.length;i++){
n[i]=Integer.parseInt(tr[i]);
}
// for(int i=0;i<n.length;i++){
// System.out.print(n[i]);
// }
//排序
Arrays.sort(n);
//有格式输出
StringBuilder sb=new StringBuilder();
for(int i=0;i<n.length;i++){
if(i==n.length-1){
sb.append(n[i]);
}else{
sb.append(n[i]).append(" ");
}
}
String str=sb.toString();
System.out.println(str);
}
装箱和拆箱
Date
public static void main(String[] args) {
Calendar c=Calendar.getInstance();
int i = c.get(Calendar.YEAR);
int i1 = c.get(Calendar.MARCH);
int i2 = c.get(Calendar.DATE);
System.out.println(i+"年"+i1+"月"+i2+"日");
}
案例:二月天
public static void main(String[] args) {
//输入任意年份
Scanner in=new Scanner(System.in);
System.out.println("输入年份");
int year=in.nextInt();
//设置对象的年月日
Calendar c=Calendar.getInstance();
c.set(year,2,1);
c.add(Calendar.DATE,-1);
int i = c.get(Calendar.DATE);
System.out.println("二月份共有:"+i);
}
集合
案例
存储字符串并遍历
public static void main(String[] args) {
ArrayList<String> ar=new ArrayList<String>();
ar.add("xuge");
ar.add("yuge");
ar.add("儿子建");
ar.add("死鬼懂");
for (int i=0;i<ar.size();i++){
System.out.println(ar.get(i));
}
}
存储学生对象并遍历
存储学生对象并遍历
public class Student1 {
private String name;
private int age;
/* public Student1(String name,int age){
this.name =name;
this.age=age;
}*/
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;
}
}
import java.util.ArrayList;
import java.util.Scanner;
public class ArrayListDome4 {
public static void main(String[] args) {
/*Scanner in=new Scanner(System.in);
System.out.println("请输入名字:");
String name=in.nextLine();
System.out.println("请输入年龄:");
int age=in.nextInt();
Student1 sc=new Student1();
sc.setName(name);
sc.setAge(age);
ArrayList<Student1> array=new ArrayList<Student1>();
array.add(sc);*/
ArrayList<Student1> array=new ArrayList<Student1>();
addStudent(array);
addStudent(array);
addStudent(array);
for(int i=0;i<array.size();i++){
Student1 s = array.get(i);
System.out.println(s.getName()+","+s.getAge());
}
}
//增加学生方法
public static void addStudent(ArrayList<Student1> array){
Scanner in=new Scanner(System.in);
System.out.println("请输入名字:");
String name=in.nextLine();
System.out.println("请输入年龄:");
int age=in.nextInt();
Student1 sc=new Student1();
sc.setName(name);
sc.setAge(age);
array.add(sc);
}
}
学生管理系统
思路
public class Student {
private String id;
private String name;
private String age;
private String address;
public Student() {
}
public Student(String id, String name, String age, String address) {
this.id = id;
this.name = name;
this.age = age;
this.address = address;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAge() {
return age;
}
public void setAge(String age) {
this.age = age;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
}
public class Manager {
public static void main(String[] args) {
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。退出");
System.out.println("请输入你的选择:");
Scanner in =new Scanner(System.in);
String s=in.nextLine();
switch (s) {
case "1":
System.out.println("添加学生");
break;
case "2":
System.out.println("删除学生");
break;
case "3":
System.out.println("修改学生");
break;
case "4":
System.out.println("查看所有学生");
break;
case "5":
System.out.println("退出");
System.exit(0);
default:
System.out.println("你输入有误");
}
}
}
}
public static void addStudent(ArrayList<Student> array){
//输入学生信息
Scanner in=new Scanner(System.in);
System.out.println("请输入添加的学号");
String id=in.nextLine();
System.out.println("请输入姓名");
String name=in.nextLine();
System.out.println("请输入年龄");
String age=in.nextLine();
System.out.println("请输入地址");
String address=in.nextLine();
//添加学生信息赋值给成员变量
Student sc=new Student();
sc.setId(id);
sc.setName(name);
sc.setAge(age);
sc.setAddress(address);
//将学生对象添加到集合中
array.add(sc);
//给出成功添加提示
System.out.println("添加成功");
}
public static void findAllStudent(ArrayList<Student>array){
System.out.println("学号\t\t姓名\t年龄\t地址");
for (int i=0;i<array.size();i++){
Student s = array.get(i);
System.out.println(s.getId()+"\t"+s.getName()+"\t"+s.getAge()+"\t"+s.getAddress());
}
}
public static void delectStudent(ArrayList<Student> array){
Scanner in=new Scanner(System.in);
System.out.println("请输入你要删除的学号:");
String id=in.nextLine();
for(int i=0;i<array.size();i++){
Student s = array.get(i);
if(s.getId().equals(id)){
array.remove(i);
break;
}
}
System.out.println("删除成功");
}
public static void updateStudent(ArrayList<Student> array){
//输入要修改的学生信息
Scanner in=new Scanner(System.in);
System.out.println("输入要修改的学生ID");
String id=in.nextLine();
System.out.println("输入要修改的学生姓名");
String name=in.nextLine();
System.out.println("输入要修改的学生年龄");
String age=in.nextLine();
System.out.println("输入要修改的学生地址");
String address=in.nextLine();
//创建学生对象
Student student=new Student();
student.setId(id);
student.setName(name);
student.setAge(age);
student.setAddress(address);
//遍历集合修改对应学生信息
for(int i=0;i<array.size();i++){
Student s = array.get(i);
if(s.getId().equals(id)){
array.set(i,student);
break;
}
}
System.out.println("修改成功");
}
public static void delectStudent(ArrayList<Student> array){
Scanner in=new Scanner(System.in);
System.out.println("请输入你要删除的学号:");
String id=in.nextLine();
int index=-1;
for(int i=0;i<array.size();i++){
Student s = array.get(i);
if(s.getId().equals(id)){
index=i;
break;
}
}
if (index==-1){
System.out.println("你输入有误,请重新输入");
}else{
array.remove(index);
System.out.println("删除成功");
}
}
|