如何在 Java 中静态初始化对象数组
How to Statically Initialize Array Of Objects in Java
如何静态初始化 Java 中的对象数组?
我的意图的一个简单示例如下:
说我的对象是
public class FRIEND {
String name;
String phone;
}
我的意图是做一些像下面这样的事情,但它不起作用(我试过变体但没有成功)。
static FRIEND[] MyFriends[] = {
{ "Bob", "5551234" },
{ "Jack", "5716666" },
{ "Mary", "5341111" }
};
我发现了大量数组初始化的示例,但它们要么 (a) 初始化基元数组(例如 int[]),要么 (b) 使用具有显式设置的循环(例如 MyFriends[n].name = "Bob");
我做错了什么?
创建 FRIEND
个对象
static FRIEND[] MyFriends = {
new FRIEND("Bob", "5551234"),
new FRIEND("Jack", "5716666"),
new FRIEND("Mary", "5341111")
};
向 FRIEND
添加一个构造函数 class
public class FRIEND {
String name;
String phone;
public FRIEND(String name, String phone) {
this.name = name;
this.phone = phone;
}
}
完整代码(java 8)
import java.util.Arrays;
class FRIEND {
String name;
String phone;
static FRIEND[] MyFriends = {
new FRIEND("Bob", "5551234"),
new FRIEND("Jack", "5716666"),
new FRIEND("Mary", "5341111")
};
public FRIEND(String name, String phone) {
this.name = name;
this.phone = phone;
}
@Override
public String toString() {
return "FRIEND{" +
"name='" + name + '\'' +
", phone='" + phone + '\'' +
'}';
}
public static void main(String[] args) {
Arrays.stream(MyFriends).forEach(System.out::println);
}
}
输出
FRIEND{name='Bob', phone='5551234'}
FRIEND{name='Jack', phone='5716666'}
FRIEND{name='Mary', phone='5341111'}
也许你也可以使用这样的枚举:
public enum Friend
{
BOB("Bob", "5551234"),
JACK("Jack", "5716666"),
MARY("Mary", "5341111");
private String name;
private String phone;
Friend(String name, String phone) {
this.name = name;
this.phone = phone;
}
public String getName() {
return name;
}
public String getPhone() {
return phone;
}
}
是另一个可以使用的选项。
希望对您有所帮助。
按照 in Oracle code conventions "class names should be nouns, in mixed case with the first letter of each internal word capitalized" 的规定,因此最好使用 Friend
作为您的 class 名称,而不是 FRIEND
。
您的静态 MyFriends
数组将被重写为
public static Friend[] MyFriends = {
new Friend("Bob", "5551234"),
new Friend("Jack", "5716666"),
new Friend("Mary", "5341111")
}
上面的代码Friend
class:
package Whosebug;
public class Friend {
String name;
String phone;
public Friend(String name, String phone) {
this.name = name;
this.phone = phone;
}
@Override
public String toString() {
return "Friend [name=" + name + ", phone=" + phone + "]";
}
public static Friend[] MyFriends = {
new Friend("Bob", "5551234"),
new Friend("Jack", "5716666"),
new Friend("Mary", "5341111")
}
public static void main(String[] args) {
for (Friend friend : MyFriends) {
System.out.println(friend);
}
}
}
如何静态初始化 Java 中的对象数组? 我的意图的一个简单示例如下:
说我的对象是
public class FRIEND {
String name;
String phone;
}
我的意图是做一些像下面这样的事情,但它不起作用(我试过变体但没有成功)。
static FRIEND[] MyFriends[] = {
{ "Bob", "5551234" },
{ "Jack", "5716666" },
{ "Mary", "5341111" }
};
我发现了大量数组初始化的示例,但它们要么 (a) 初始化基元数组(例如 int[]),要么 (b) 使用具有显式设置的循环(例如 MyFriends[n].name = "Bob");
我做错了什么?
创建 FRIEND
个对象
static FRIEND[] MyFriends = {
new FRIEND("Bob", "5551234"),
new FRIEND("Jack", "5716666"),
new FRIEND("Mary", "5341111")
};
向 FRIEND
添加一个构造函数 class
public class FRIEND {
String name;
String phone;
public FRIEND(String name, String phone) {
this.name = name;
this.phone = phone;
}
}
完整代码(java 8)
import java.util.Arrays;
class FRIEND {
String name;
String phone;
static FRIEND[] MyFriends = {
new FRIEND("Bob", "5551234"),
new FRIEND("Jack", "5716666"),
new FRIEND("Mary", "5341111")
};
public FRIEND(String name, String phone) {
this.name = name;
this.phone = phone;
}
@Override
public String toString() {
return "FRIEND{" +
"name='" + name + '\'' +
", phone='" + phone + '\'' +
'}';
}
public static void main(String[] args) {
Arrays.stream(MyFriends).forEach(System.out::println);
}
}
输出
FRIEND{name='Bob', phone='5551234'}
FRIEND{name='Jack', phone='5716666'}
FRIEND{name='Mary', phone='5341111'}
也许你也可以使用这样的枚举:
public enum Friend
{
BOB("Bob", "5551234"),
JACK("Jack", "5716666"),
MARY("Mary", "5341111");
private String name;
private String phone;
Friend(String name, String phone) {
this.name = name;
this.phone = phone;
}
public String getName() {
return name;
}
public String getPhone() {
return phone;
}
}
是另一个可以使用的选项。
希望对您有所帮助。
按照 in Oracle code conventions "class names should be nouns, in mixed case with the first letter of each internal word capitalized" 的规定,因此最好使用 Friend
作为您的 class 名称,而不是 FRIEND
。
您的静态 MyFriends
数组将被重写为
public static Friend[] MyFriends = {
new Friend("Bob", "5551234"),
new Friend("Jack", "5716666"),
new Friend("Mary", "5341111")
}
上面的代码Friend
class:
package Whosebug;
public class Friend {
String name;
String phone;
public Friend(String name, String phone) {
this.name = name;
this.phone = phone;
}
@Override
public String toString() {
return "Friend [name=" + name + ", phone=" + phone + "]";
}
public static Friend[] MyFriends = {
new Friend("Bob", "5551234"),
new Friend("Jack", "5716666"),
new Friend("Mary", "5341111")
}
public static void main(String[] args) {
for (Friend friend : MyFriends) {
System.out.println(friend);
}
}
}