当前位置:首页 » 编程语言 » 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
加载更多

热点内容
bs源码 发布:2025-02-01 16:51:52 浏览:566
百度云zip怎么解压 发布:2025-02-01 16:31:59 浏览:566
pmc密码是什么意思 发布:2025-02-01 16:28:49 浏览:783
苹果手表和安卓手表哪个好用 发布:2025-02-01 16:28:07 浏览:582
上海电信上传 发布:2025-02-01 16:22:10 浏览:982
有什么好的双u主板配置 发布:2025-02-01 16:18:17 浏览:177
编程漩涡 发布:2025-02-01 16:01:51 浏览:328
司机会所访问 发布:2025-02-01 15:54:11 浏览:780
家用电脑改成服务器并让外网访问 发布:2025-02-01 15:30:23 浏览:355
javac工资 发布:2025-02-01 15:24:28 浏览:23