❶ java怎么创建一个数组,每个元素是一个数组
用 ArrayList 里 放数组吧,ArrayList 本身就是一数组类型存放的,你那种现在还不行,除非你自己封装一个,数组的方法。
❷ 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中怎么创建一个数组
Java 中创建数组的基本格式为 type[] varname = new type[size]{item1, item2, item3},其中 type 表示元素的类型, size 表示创建数组的大小,在指定后面所有元素的情况下,这个大小可以省略,后面花括号括起来的部分,用于指定元素,如果指定了大小,可以不要后面的部分,如以下语句军创建了一个数组;
int[] = new int[1]; // 创建一个长度为1 的整形数组
int[] = new []{1}; // 创建一个长度为1,第一个元素的值为1;