java實現表格數據的存儲

本文實例為大傢分享瞭java實現表格數據存儲的具體代碼,供大傢參考,具體內容如下

一、二維數組實現表格數據的存儲

import java.util.Arrays;

/**
 * 使用Object[][]存儲表格信息
 * @author Lhk
 *
 */
public class Array_Test01 {
 public static void main(String args[]){
 //一個Object[]數組代表表格的一行數據
 //此處的基本數據類型1001,1002,1003本質不是Object對象,Java編譯會自動把基本數據類型“自動裝箱”成包裝類對象
 Object[] emp0={1001,"明明",20,"程序員","2021-03-28"};
 Object[] emp1={1002,"花花",21,"程序員","2021-02-28"};
 Object[] emp2={1002,"小李",19,"程序員","2021-04-01"};
 
 Object[][] emps=new Object[3][];
 emps[0]=emp0;
 emps[1]=emp1;
 emps[2]=emp2;
 
 //遍歷數組(可用嵌套循環遍歷多維數組),輸出表格
 for(int i=0;i<emps.length;i++){
  System.out.println(Arrays.toString(emps[i]));
 }
 
 }
}

二、Javabean和數組存儲表格信息

import java.util.Arrays;

/**
 * 使用javabean和數組存儲表格信息
 * @author Lhk
 *
 */
public class Array_Test02 {
 public static void main(String args[]) {
 Emps emp0=new Emps(1001,"明明",20,"程序員","2021-03-28");
 Emps emp1=new Emps(1002,"花花",21,"程序員","2021-02-28");
 Emps emp2=new Emps(1002,"小李",19,"程序員","2021-04-01");
 
 Emps[] emps={emp0,emp1,emp2};//靜態初始化
 
 for(int i=0;i<emps.length;i++){
  System.out.println(emps[i].toString());
 }
 }

}

class Emps {
 private int id;
 private String name;
 private int age;
 private String job;
 private String hiretime;
 
 public Emps(int id, String name, int age, String job, String hiretime) {
 super();
 this.id = id;
 this.name = name;
 this.age = age;
 this.job = job;
 this.hiretime = hiretime;
 }

 public Emps() {
 super();
 }

 
 public int getId() {
 return id;
 }

 public void setId(int id) {
 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 String getJob() {
 return job;
 }

 public void setJob(String job) {
 this.job = job;
 }

 public String getHiretime() {
 return hiretime;
 }

 public void setHiretime(String hiretime) {
 this.hiretime = hiretime;
 }

 
 @Override
 public String toString() {
 //重寫toString()方法可以用get方法來返回屬性
 return "emps [id=" + getId() + ", name=" + getName() + ", age=" + getAge() + ", job=" + getJob() + ", Hiretime=" + getHiretime() + "]";
 }
 
}

以上就是本文的全部內容,希望對大傢的學習有所幫助,也希望大傢多多支持WalkonNet。

推薦閱讀:

    None Found