案例学习java
对象数组学习1:
//商品存储
public class Good{
private String id;
private String name;
private double price;
private int count;
public Good() {
}
public Good(String id, String name, double price, int count) {
this.id = id;
this.name = name;
this.price = price;
this.count = count;
}
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 double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
public int getCount() {
return count;
}
public void setCount(int count) {
this.count = count;
}
}
public class goodTest {
public static void main(String[] args) {
Good[] goods=new Good[3];
Good g1=new Good("001","华为p40",5999.0,100);
Good g2=new Good("002","保温杯",227.0,50);
Good g3=new Good("003","枸杞",21.0,70);
goods[0]=g1;
goods[1]=g2;
goods[2]=g3;
for (int i = 0; i <goods.length ; i++) {
Good g=goods[i];
System.out.println(g.getId()+","+g.getName()+","+g.getPrice()+","+g.getCount());
}
}
}
键盘录入知识点:
第二套:遇到回车才停止接收数据。
两个体系不要混用。
对象数组学习2
//存储汽车信息
public class Car{
private String name;
private int price;
private String color;
public Car() {
}
public Car(String name, int price, String color) {
this.name = name;
this.price = price;
this.color = color;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getPrice() {
return price;
}
public void setPrice(int price) {
this.price = price;
}
public String getColor() {
return color;
}
public void setColor(String color) {
this.color = color;
}
}
public class carTest {
public static void main(String[] args) {
Car[] cars=new Car[3];
Scanner sc =new Scanner(System.in);
for (int i = 0; i <cars.length ; i++) {
Car c=new Car();
System.out.println("输入汽车品牌");
String name=sc.next();
c.setName(name);
System.out.println("输入汽车价格");
int price=sc.nextInt();
c.setPrice(price);
System.out.println("输入汽车颜色");
String color=sc.next();
c.setColor(color);
cars[i]=c;
}
for (int i = 0; i <cars.length ; i++) {
Car c=cars[i];
System.out.println(c.getName()+","+c.getPrice()+","+c.getColor());
}
}
}
复杂对象数组练习
数组长度为3,存储3名学生对象最为初始数据, 要求1,添加一个学生,要做学号唯一性判断,要求2,添加完毕,遍历所有学生
//存储学生信息
public class Student{
private String name;
private int age;
private int id;//字符串比较还没学,id暂时是int类型
public Student() {
}
public Student(String name, int age, int id) {
this.name = name;
this.age = age;
this.id = id;
}
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;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
}
//数组长度为3,存储3名学生对象最为初始数据,
//要求1,添加一个学生,要做学号唯一性判断,要求2,添加完毕,遍历所有学生
//要求3,通过id删除学生信息,存在,删除,不存在,提示删除失败。要求4,删除完毕后,遍历所有学生,要求5,查询id‘002’的学生,如果存在,年龄加1
public class studentTest {
public static void main(String[] args) {
Student[] student=new Student[3];
Student s1=new Student("小王",15,001);
Student s2=new Student("小明",14,002);
Student s3=new Student("小刘",16,003);
student[0]=s1;
student[1]=s2;
student[2]=s3;
Student s4=new Student("张三",15,004);
boolean flag=exitID(student,s4.getId());
Student[] newStu=newStudent(student);
if(flag){
System.out.println("学号重复,重新输入");
}
else {
if (getSize(student) == student.length) {
//增加一个位置,把原数组复制过去
newStu[getSize(student)] = s4;
printArr(newStu);
} else {
//直接加进去,要判断一下学号
student[getSize(student)] = s4;
printArr(student);
}
}
}
public static int getSize(Student[] arr){
int count=0;
for (int i = 0; i <arr.length ; i++) {
if(arr[i]!=null)
count++;
}
return count;
}
public static Student[] newStudent(Student[] arr){
Student[] newStu=new Student[arr.length+1];
for (int i = 0; i <arr.length ; i++) {
newStu[i]=arr[i];
}
return newStu;
}
public static boolean exitID(Student[] arr,int id ){
for (int i = 0; i <arr.length ; i++) {
if(arr[i]!=null){
Student s=arr[i];
if(s.getId()==id){
return true;
}
}
}
return false;
}
public static void printArr(Student[] arr){
for (int i = 0; i <arr.length ; i++) {
Student s=arr[i];
if(s!=null){
System.out.println(s.getName()+","+s.getAge()+","+s.getId());
}
}
}
}
要求3,通过id删除学生信息,存在,删除,不存在,提示删除失败。要求4,删除完毕后,遍历所有学生
//数组长度为3,存储3名学生对象最为初始数据,
//要求1,添加一个学生,要做学号唯一性判断,要求2,添加完毕,遍历所有学生
//要求3,通过id删除学生信息,存在,删除,不存在,提示删除失败。要求4,删除完毕后,遍历所有学生,要求5,查询id‘002’的学生,如果存在,年龄加1
public class studentTest {
public static void main(String[] args) {
Student[] student=new Student[3];
Student s1=new Student("小王",15,001);
Student s2=new Student("小明",14,002);
Student s3=new Student("小刘",16,003);
student[0]=s1;
student[1]=s2;
student[2]=s3;
// Student s4=new Student("张三",15,004);
// boolean flag=exitID(student,s4.getId());
// Student[] newStu=newStudent(student);
// if(flag){
// System.out.println("学号重复,重新输入");
// }
// else {
// if (getSize(student) == student.length) {
// //增加一个位置,把原数组复制过去
//
// newStu[getSize(student)] = s4;
//
// printArr(newStu);
//
// } else {
// //直接加进去,要判断一下学号
//
// student[getSize(student)] = s4;
//
// printArr(student);
// }
// }
int index=getIndex(student,2);
if(index<0){
System.out.println("x学号不存在");
}else{
student[index]=null;
printArr(student);
}
}
public static int getIndex(Student[] arr,int id){
for (int i = 0; i <arr.length ; i++) {
if(arr[i]!=null){
Student s=arr[i];
if(s.getId()==id)
return i;
}
}
return -1;
}
public static int getSize(Student[] arr){
int count=0;
for (int i = 0; i <arr.length ; i++) {
if(arr[i]!=null)
count++;
}
return count;
}
public static Student[] newStudent(Student[] arr){
Student[] newStu=new Student[arr.length+1];
for (int i = 0; i <arr.length ; i++) {
newStu[i]=arr[i];
}
return newStu;
}
public static boolean exitID(Student[] arr,int id ){
for (int i = 0; i <arr.length ; i++) {
if(arr[i]!=null){
Student s=arr[i];
if(s.getId()==id){
return true;
}
}
}
return false;
}
public static void printArr(Student[] arr){
for (int i = 0; i <arr.length ; i++) {
Student s=arr[i];
if(s!=null){
System.out.println(s.getName()+","+s.getAge()+","+s.getId());
}
}
}
}
要求5:查询id‘002’的学生,如果存在,年龄加1
//数组长度为3,存储3名学生对象最为初始数据,
//要求1,添加一个学生,要做学号唯一性判断,要求2,添加完毕,遍历所有学生
//要求3,通过id删除学生信息,存在,删除,不存在,提示删除失败。要求4,删除完毕后,遍历所有学生,要求5,查询id‘002’的学生,如果存在,年龄加1
public class studentTest {
public static void main(String[] args) {
Student[] student=new Student[3];
Student s1=new Student("小王",15,001);
Student s2=new Student("小明",14,002);
Student s3=new Student("小刘",16,003);
student[0]=s1;
student[1]=s2;
student[2]=s3;
int index=getIndex(student,2);
if(index>=0){
Student s=student[index];
int newAge=s.getAge()+1;
s.setAge(newAge);
printArr(student);
}else{
System.out.println("所查学号不存在");
}
// Student s4=new Student("张三",15,004);
// boolean flag=exitID(student,s4.getId());
// Student[] newStu=newStudent(student);
// if(flag){
// System.out.println("学号重复,重新输入");
// }
// else {
// if (getSize(student) == student.length) {
// //增加一个位置,把原数组复制过去
//
// newStu[getSize(student)] = s4;
//
// printArr(newStu);
//
// } else {
// //直接加进去,要判断一下学号
//
// student[getSize(student)] = s4;
//
// printArr(student);
// }
// }
// int index=getIndex(student,2);
// if(index<0){
// System.out.println("x学号不存在");
// }else{
// student[index]=null;
// printArr(student);
// }
}
public static int getIndex(Student[] arr,int id){
for (int i = 0; i <arr.length ; i++) {
if(arr[i]!=null){
Student s=arr[i];
if(s.getId()==id)
return i;
}
}
return -1;
}
public static int getSize(Student[] arr){
int count=0;
for (int i = 0; i <arr.length ; i++) {
if(arr[i]!=null)
count++;
}
return count;
}
public static Student[] newStudent(Student[] arr){
Student[] newStu=new Student[arr.length+1];
for (int i = 0; i <arr.length ; i++) {
newStu[i]=arr[i];
}
return newStu;
}
public static boolean exitID(Student[] arr,int id ){
for (int i = 0; i <arr.length ; i++) {
if(arr[i]!=null){
Student s=arr[i];
if(s.getId()==id){
return true;
}
}
}
return false;
}
public static void printArr(Student[] arr){
for (int i = 0; i <arr.length ; i++) {
Student s=arr[i];
if(s!=null){
System.out.println(s.getName()+","+s.getAge()+","+s.getId());
}
}
}
}