當前位置:首頁 » 編程語言 » 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修飾,分開的話可以。

熱點內容
編譯opengl 發布:2024-04-26 21:57:32 瀏覽:506
ubuntu查找文件夾 發布:2024-04-26 21:48:18 瀏覽:203
qq瀏覽器wifi助手怎麼查看密碼 發布:2024-04-26 21:48:18 瀏覽:669
在線安裝androidsdk 發布:2024-04-26 21:42:33 瀏覽:480
杭州地鐵wifi密碼是多少 發布:2024-04-26 21:32:45 瀏覽:320
重裝系統源碼 發布:2024-04-26 21:32:44 瀏覽:275
國外大叔解壓視頻 發布:2024-04-26 20:44:00 瀏覽:228
存儲念第幾音 發布:2024-04-26 20:33:35 瀏覽:251
衡陽dns的伺服器地址是多少 發布:2024-04-26 20:32:26 瀏覽:270
我的世界空島伺服器青金石 發布:2024-04-26 20:18:03 瀏覽:650