導航:首頁 > 編程語言 > java對象數組

java對象數組

發布時間:2022-02-01 17:01:08

java類怎樣定義數組對象數組

public class a {
public static void main(String[]args){ int a[]={3,9,8};//這個是數組的靜態初始化. Date days[]={new Date(1,4,2994),new Date(2,4,2004),new Date(2,5,2005)};
//創建了3個Date對象放在days[]數組里。 //這里還有種寫法。你可以先定義個數組,然後動態的進行付值。 //這樣寫可能煩了點,你也可以用for循環來進行動態賦值。 //列:Date days[]; // days=new Date[3]; // days[0]=new Date(1,2,3); // days[1]=new Date(1,2,3); // days[2]=new Date(1,2,3);
for(int i=0;i<days.length;i++){ //循環數組里的對象
//將對象中的a屬性列印輸出。

② java對象數組如何調用

mian是程序的入口,如果你想在mian裡面初始化,之後在其他的方法裡面調用,要麼把這個數組聲明成static的,要麼在之後的方法裡面通過參數將數組對象傳遞。

③ JAVA中的對象數組

其實 只要會定義數組了 其他的都不難
數組邊聲明邊賦值定義方式有兩種
int [ ] 數組名 = {1,2,3,4,5};
這個是變定義變賦值
int [ ] 數組名 = new int[ ]{1,2,3,4,5};

僅聲明 方式如下 (整型數組):
int [ ] 數組名 = new int [數組下標 ];

樓主 不如加下我群吧 大家一起討論java
群號:173686507

④ java中怎麼創建對象數組

//如果你是初學JAVA,建議你看看這段代碼,裡面有一些基本技巧
//有疑問可以問我!

import java.io.*;

public class StudentInformation {

public static void main(String args[]) throws IOException {
Student[] st = new Student[5];// 第一個錯誤
int i = 0;
st[i++] = new Student().setStudent("WangZhen", 20, 1, 1.76);
st[i++] = new Student().setStudent("YangZhen", 21, 2, 1.66);
st[i++] = new Student().setStudent("Wangqiang", 19, 3, 1.86);
st[i++] = new Student().setStudent("XiaoMing", 18, 4, 1.71);
st[i++] = new Student().setStudent("XiaoHong", 22, 5, 1.74);
for (i = 0; i < st.length; i++)
st[i].display();
System.out.println("請輸入要查詢學生的姓名");
String str;
BufferedReader buf;
buf = new BufferedReader(new InputStreamReader(System.in));
str = buf.readLine();
for (i = 0; i < st.length; i++) {
if (st[i].name.equals(str)) {
System.out.println("所要查找學生為");
st[i].display();
}
}

}

}

class Student {
String name;

int age;

int num;// 學號

double high;// 身高

public void display() {
System.out.println("姓名:" + name + "\t年齡:" + age + "\t身高:" + high + "\t學號:" + num);
}

public Student setStudent(String n, int a, int nu, double h) {
name = n;
age = a;
num = nu;
high = h;
return this;
}
}

⑤ java對象數組

public class Student
{

private String username;
private int num;

public Student(String username, int num)
{
this.username = username;
this.num = num;
}

public static void main(String[] args)
{
Student s[] = new Student[10];
for (int i = 0; i < s.length; i++)
{
s[i] = new Student(i + "", i);
System.out.println(s[i]);
}

}

public String getUsername()
{
return username;
}

public void setUsername(String username)
{
this.username = username;
}

public int getNum()
{
return num;
}

public void setNum(int num)
{
this.num = num;
}

@Override
public String toString()
{
return this.num + " " + this.username;
}

}

⑥ 什麼是java對象數組

您好,提問者:
對象數組含義:其實String就是一個對象數組,因為String是引用類型,是個類。

classPerson{
privateStringname;
privateintage;
publicPerson(Stringname,intage){
this.name=name;
this.age=age;
}
publicStringgetName(){
returnname;
}
publicintgetAge(){
returnage;
}
}
//對象數組測試類
publicclassDemo{
publicstaticvoidmain(String[]args){
Person[]arr=newPerson[3];
arr[0]=newPerson("對象引用001",21);
arr[1]=newPerson("對象引用002",22);
//其實對象數組存的都是對象的引用地址
for(inti=0;i<arr.length;i++){
Personp=arr[i];
System.out.println("姓名:"+p.getName+" 年齡:"+p.getAge());
}
}
}

//這里說明一下,其實List、Set、Map就是對象集合。

//而ArrayList底層就是對象數組。。。。

⑦ java 對象數組定義

寫了一個簡單的實例,你可以參考一下。
public class a {
public static void main(String[]args){

int a[]={3,9,8};//這個是數組的靜態初始化.

Date days[]={new Date(1,4,2994),new Date(2,4,2004),new Date(2,5,2005)};
//創建了3個Date對象放在days[]數組里。

//這里還有種寫法。你可以先定義個數組,然後動態的進行付值。

//這樣寫可能煩了點,你也可以用for循環來進行動態賦值。

//列:Date days[];

// days=new Date[3];

// days[0]=new Date(1,2,3);

// days[1]=new Date(1,2,3);

// days[2]=new Date(1,2,3);

for(int i=0;i<days.length;i++){

//循環數組里的對象
System.out.println(days[i].a);
//將對象中的a屬性列印輸出。

}
}
}

class Date{
int a,b,c;
Date(int x,int y,int z){
a=x;
b=y;
z=c;
}
}

⑧ java 對象數組定義是什麼

對象是類的一個實例(對象不是找個女朋友),有狀態和行為。例如,一條狗是一個對象,它的狀態有:顏色、名字、品種;行為有:搖尾巴、叫、吃等。

數組的三種定義方法

1.數組類型[] 數組名=new 數組類型[數組長度];

2.數組類型[] 數組名={數組0,數組1,數組2,數組3,....};

3.數組類型[] 數組名=new 數組類型[]{數組0,數組1,數組2,...};

⑨ 關於Java中的對象數組

Data []d=new Data[3];
這里只是實例化了對象d 並沒有實例化了 d[I]等對象 也就是你只是給 d分配了內存 但是d[I]並沒有分配內存當你d[0].getData();的時候當然出錯拉
d[i]=new Data();就是給每個d[i]對象賦值也就是給d[I]對象的地址=new Data() 這個新分配的地址

閱讀全文

與java對象數組相關的資料

熱點內容
androidm3u8緩存 瀏覽:234
imphp開源知乎 瀏覽:706
清除網路通配符dos命令 瀏覽:837
鴻蒙系統怎麼快速換回安卓 瀏覽:712
pdf綠色虛擬列印機 瀏覽:213
androidtab框架 瀏覽:147
java轉php的時間戳 瀏覽:639
編譯libstdc依賴 瀏覽:658
清演算法人與原法人的區別 瀏覽:410
家庭裝修下載什麼app軟體 瀏覽:575
美食博主用什麼app拍視頻 瀏覽:815
ipone手機如何加密微信 瀏覽:357
自來水加密閥閥帽 瀏覽:437
華為交換機dhcp配置命令 瀏覽:319
androidbitmap縮小 瀏覽:275
單片機串口控制燈 瀏覽:88
大訊雲伺服器安裝視頻 瀏覽:788
華為演算法領先世界 瀏覽:658
linux路由重啟 瀏覽:570
php的模板編程 瀏覽:324