当前位置:首页 » 编程语言 » java中对象数组

java中对象数组

发布时间: 2022-08-11 03:57:45

java中对象数组请大家帮忙解释一下

对象数组就是数组里的每个对象都是类的对象,赋值时先定义对象,然后将对象直接赋给数组!

❷ java 对象数组定义是什么

对象是类的一个实例(对象不是找个女朋友),有状态和行为。例如,一条狗是一个对象,它的状态有:颜色、名字、品种;行为有:摇尾巴、叫、吃等。

数组的三种定义方法

1.数组类型[] 数组名=new 数组类型[数组长度];

2.数组类型[] 数组名={数组0,数组1,数组2,数组3,....};

3.数组类型[] 数组名=new 数组类型[]{数组0,数组1,数组2,...};

❸ 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中对象和数组有什么联系和区别,说详细一点

对象和数组是两个不同领域的概念,他们之间没有可比较性

一..对象,简单的说就是一个Class的实例,创建一个对象,最常见的方式是new 如:new MyClass()
1.但是MyClass里面可以有数组,那么这个数组就是这个类的属性
2. Class MyClass{
private Integer[]arr = {1,2,3};
private String [] brr = {"b","c","d"};
private YourClass p[ crr = {new YourClass(),new YourClass()};

}

二,数组,一个数组,里面可以放多个基本数据类型或对象,如:

private YourClass p[ crr = {new YourClass(),new YourClass()};

❺ 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里面,什么是对象数组

就是内部元素全都是对象的数组啊
int[] 整型数组,里面的元素都是int类型的
double[] 浮点数数组,里面的元素都是double类型的浮点数
Object[] Object对象数组,里面的元素都是Object类的对象

❼ java中怎么创建对象数组

首先我们需要创建一个class:

classStudent{
Stringname;
doublescore;
Stringnum;

Student(Stringn,doubles,Stringm){
name=n;
s=score;
num=m;
}

publicstaticvoidprintInfo(){
System.out.println(num+","+name+","+score);
}

}

接下来我们对此类进行数组的创建:

//1
Studentstu[];<spanstyle="white-space:pre"></span>//声明数组。
stu=newStudent[3];<spanstyle="white-space:pre"></span>//创建数组,这里是创建的一个引用的数组,每一个引用并没有确切的地址。
for(inti=0;i<3;i++){<spanstyle="white-space:pre"></span>//为数组创建对象,也就是说为创建的引用关联到确切的地址。
stu[i]=newStudent();
}
//2
Studentstu[]=newStudent[3];
for(inti=0;i<3;i++){
stu[i]=newStudent();
}
//3
Studentstu[]=newStudent{newStudent(sjl,87,01),newStudent(ljs,98,02),newStudent(lls,92,03)};

❽ 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修饰,分开的话可以。

热点内容
点赞熊源码 发布:2024-05-08 08:49:44 浏览:457
压缩性胸闷 发布:2024-05-08 08:18:18 浏览:528
电脑哔哩哔哩pc端的缓存 发布:2024-05-08 08:17:05 浏览:171
王者服务器崩溃估计什么时候好 发布:2024-05-08 08:16:51 浏览:575
qj压缩机 发布:2024-05-08 08:10:13 浏览:842
dhcp服务器可以分配什么参数 发布:2024-05-08 08:07:36 浏览:959
跨象限编程 发布:2024-05-08 07:58:37 浏览:62
java多对一 发布:2024-05-08 07:58:33 浏览:642
苹果怎么创建文件夹 发布:2024-05-08 07:53:34 浏览:918
html连接sql数据库 发布:2024-05-08 07:53:28 浏览:737