App下載

如何實現(xiàn)Java中的表格數(shù)據(jù)存儲? 存儲方式有哪些?

猿友 2021-07-15 14:36:55 瀏覽數(shù) (2629)
反饋

學習過數(shù)組的小伙伴應該了解了一維數(shù)組的存儲方式,但是對于表格類數(shù)據(jù)的存儲,一維數(shù)組的存儲就不合適了。本篇文章將介紹兩種用Java代碼實現(xiàn)表格數(shù)據(jù)的存儲。以下是詳情內容。

一、二維數(shù)組實現(xiàn)表格數(shù)據(jù)的存儲

import java.util.Arrays;

/**
 * 使用Object[][]存儲表格信息
 * @author Lhk
 *
 */
public class Array_Test01 {
 public static void main(String args[]){
 //一個Object[]數(shù)組代表表格的一行數(shù)據(jù)
 //此處的基本數(shù)據(jù)類型1001,1002,1003本質不是Object對象,Java編譯會自動把基本數(shù)據(jù)類型“自動裝箱”成包裝類對象
 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;
 
 //遍歷數(shù)組(可用嵌套循環(huán)遍歷多維數(shù)組),輸出表格
 for(int i=0;i<emps.length;i++){
  System.out.println(Arrays.toString(emps[i]));
 }
 
 }
}

二、Javabean和數(shù)組存儲表格信息

import java.util.Arrays;

/**
 * 使用javabean和數(shù)組存儲表格信息
 * @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};//靜態(tài)初始化
 
 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() + "]";
 }
 
}

以上就是關于Java中表格數(shù)據(jù)存儲方式的全部內容,希望對大家的學習有所幫助,也希望大家多多支持W3Cschool。


0 人點贊