當前位置:首頁 » 編程語言 » java創建對象數組

java創建對象數組

發布時間: 2022-07-16 23:03:54

java對象數組創建的語法是怎麼樣的啊 舉個例子也可以

int[
]
i=new
int
[
5]
數組是引用類型
對象也是應用類型
在內存棧中存儲的是內存地址

⑵ java創建對象數組然後用構造方法實例化

Java創建對象數組然後構造方法實例化,如下:

packagecom.test;

importjava.util.Arrays;

publicclassEmployee{
/**
*員工編號
*/
privateStringnumber;

/**
*員工姓名
*/
privateStringname;

/**
*員工薪水
*/
privatedoublesalary;

/**
*無參數構造函數
*/
publicEmployee(){
System.out.println("調用了構造函數方法一,實例化對象");
}

/**
*給屬性賦值構造函數
*@paramnumber
*@paramname
*@paramsalary
*/
publicEmployee(Stringnumber,Stringname,doublesalary){
super();
this.number=number;
this.name=name;
this.salary=salary;
System.out.println("調用構造函數方法二,實例化對象");
}

publicstaticvoidmain(String[]args){
//構造Employee對象數組為2長度
Employee[]emp=newEmployee[2];

//員工一(實例化),並且構造函數里設置值
Employeee1=newEmployee("e0001","xiaoming",5000.0);


//員工二(實例化),用set設置值,get的話可以獲取到員工某個屬性
Employeee2=newEmployee();
e2.setName("小二");
e2.setNumber("e0002");
e2.setSalary(5500.1);

//將實例化的對象存進數組中
emp[0]=e1;
emp[1]=e2;
System.out.println("實例化的數組對象為:"+Arrays.toString(emp));
}

publicStringgetNumber(){
returnnumber;
}

publicvoidsetNumber(Stringnumber){
this.number=number;
}

publicStringgetName(){
returnname;
}

publicvoidsetName(Stringname){
this.name=name;
}

publicdoublegetSalary(){
returnsalary;
}

publicvoidsetSalary(doublesalary){
this.salary=salary;
}

@Override
publicStringtoString(){
return"Employee[number="+number+",name="+name+",salary="+
salary+"]";
}
}
運行結果:
調用構造函數方法二,實例化對象
調用了構造函數方法一,實例化對象
實例化的數組對象為:[Employee[number=e0001,name=xiaoming,salary=5000.0],Employee[number=e0002,name=小二,salary=5500.1]]

⑶ java定義對象數組

package com.panel.test;
import java.awt.Color;
import java.awt.Graphics;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class CreatBall extends JPanel {
private static final long serialVersionUID = 1L;
public static Balls ball[] = new Balls[2]; // 此處要初始化數組,否則在構造方法里報空指針錯誤
int x, y, radius;
Color c;
public CreatBall() {
super();
ball[0] = new Balls(10, 10, 20, Color.black);
ball[1] = new Balls(40, 40, 20, Color.blue);
}
public static void main(String args[]) {
JFrame f = new JFrame("ceshi");
f.add(new CreatBall());
f.setSize(300, 200);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setVisible(true);
}
public void paint(Graphics g) {
super.paint(g);
this.drawBall(x, y, radius, g, c);
}
public void drawBall(int x, int y, int radius, Graphics g, Color c) {
for (int i = 0; i <= 1; i++) {
g.setColor(ball[i].getColor());
g.fillOval(ball[i].getX(), ball[i].getY(), ball[i].getRadius(),
ball[i].getRadius());
}
}
}
class Balls {
private int x, y, radius;
private Color color;
Balls(int x, int y, int radius, Color color) {
this.x = x;
this.y = y;
this.radius = radius;
this.color = color;
}
public int getX() {
return x;
}
public int getY() {
return y;
}
public int getRadius() {
return radius;
}
public Color getColor() {
return color;
}
}

// Create 單詞拼錯了,不是creat,而是create.
//Balls 和 CreatBall類放在一個文件裡面的話,不能用public修飾,分開的話可以。

⑷ java如何對象創建數組並初始化

創建數組對象:int a = new int[3]
這里new指定三個操作:在堆區為數組分配空間;為數組每個元素附默認值;返回數組對象的引用。
數組對象一旦創建好了就不能改變其長度,new操作必須指定數組長度。數組長度可以為0表示無元素,直接初始化不能指定長度,例如:int a = {1,2,3}

⑸ 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中怎麼創建對象數組比如我創建了一個學生類Student,怎麼用這個類創建一個對象數組,麻煩給個例子

學生類:
class A{
private String name;
private int 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;
}

}
//學生類使用方法:
public class test(){
public static void main(String[] args) {
A[] students = new A[2]; //創建2個學生的學生數組
A as= new A();
as.setAge(15);
as.setName("tom");
A as1= new A();
as1.setAge(16);
as1.setName("cat");
A[] a={as,as1}; //動態創建學生數組
for (A st : a) {//遍歷數組
System.out.println(st.getName()+";");
}

}

}

運行結果:
tom;
cat;

⑺ 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
評論
0
0
載入更多

熱點內容
elasticsearch存儲數據 發布:2025-03-07 04:24:52 瀏覽:249
vs代碼編譯結果窗口不見了 發布:2025-03-07 04:09:25 瀏覽:437
android取消提示框 發布:2025-03-07 04:09:24 瀏覽:599
華為存儲機房安裝 發布:2025-03-07 04:08:06 瀏覽:943
adftp 發布:2025-03-07 03:42:09 瀏覽:555
雲智能編譯 發布:2025-03-07 03:37:33 瀏覽:540
大專軟體編程 發布:2025-03-07 03:37:27 瀏覽:305
倒圓怎麼編程 發布:2025-03-07 03:33:55 瀏覽:187
方舟生存進化低配置手機如何玩 發布:2025-03-07 03:31:36 瀏覽:75
安卓10和蘋果11哪個好 發布:2025-03-07 03:19:12 瀏覽:854