英文:
ArrayList & Array methods
问题
我正在编写一个简单的代码来展示有关一个人的各种细节。我已经使用数组来创建名字。
当我尝试运行我的代码时,名字的位置/目录显示为[Ljava.lang.String;@1f32e575
。
我的代码运行结果如下:
名字
ii
jj
[Ljava.lang.String;@1f32e575 // 用于移除的代码
我的代码:
public static String[] name() {
System.out.println("名字");
n = new String[]{"ii", "jj"};
for (int i = 0; i < n2.length; i++) {
System.out.println(n[i]);
}
}
英文:
I'm writing a simple code to show various details about a person. I've used arrays to create name.
When I try to run my code the location/ directory for name comes out as [Ljava.lang.String;@1f32e575
.
My code runs results are:
Name
ii
jj
[Ljava.lang.String;@1f32e575 // code to remove
My code:
public static String[] name() {
System.out.println("Name");
n = new String[]{ "ii", "jj" };
for (int i = 0; i < n2.length; i++) {
System.out.println(n[i]);
}
}
答案1
得分: 0
这是您修复后的代码:
import java.util.ArrayList;
class Detail {
private String name;
private String nationality;
private static String[] hobby2;
public Detail() {
}
public String getName() {
return name = "A";
}
public String getNationality() {
return nationality = "abc";
}
public void setInfo(String name, String nationality) {
this.name = name;
this.nationality = nationality;
}
public static void setHobbies() {
hobby2 = new String[] { "ii", "jj" };
System.out.println("Hobbies");
for (int i = 0; i < hobby2.length; i++) {
System.out.println("\t" + hobby2[i]);
}
}
public static void setWishes() {
System.out.println("Wishes");
ArrayList<String> allWishes = new ArrayList<String>();
allWishes.add("aa");
allWishes.add("bb");
allWishes.add("cc");
allWishes.add("dd");
for (String i : allWishes) {
System.out.println("\t" + i);
}
}
public void displayDetail() {
DOB d = new DOB();
System.out.println("Name: " + getName());
System.out.println("Nationality: " + getNationality());
System.out.println("Date of birth: " + d.dateString(1, "Jan", 1000));
setHobbies();
setWishes();
}
}
class DOB {
public String dateString(int day, String month, int year) {
String dob = (day + " " + month + ", " + year);
return dob;
}
}
class Test {
public static void main(String[] args) {
Detail my = new Detail();
my.displayDetail();
}
}
您不希望看到的第一个值是对象ID,您无法直接返回对象的值。如果要查看值,您需要重写toString()
方法。
第二个值之所以可见,是因为您返回了列表。如果在方法内部使用print()
,则不需要返回它。
提示:您需要更好地理解getter和setter方法,因为在代码中混淆了整个概念。您不需要空白构造函数。相反,您应该在构造函数中初始化所有字段(Detail类的类变量)。
英文:
Here, is your fixed code:
import java.util.ArrayList;
class Detail {
private String name;
private String nationality;
private static String[] hobby2;
public Detail() {
}
public String getName() {
return name = "A";
}
public String getNationality() {
return nationality = "abc";
}
public void setInfo(String name, String nationality) {
this.name = name;
this.nationality = nationality;
}
public static void setHobbies() {
hobby2 = new String[] { "ii", "jj" };
System.out.println("Hobbies");
for (int i = 0; i < hobby2.length; i++) {
System.out.println("\t" + hobby2[i]);
}
}
public static void setWishes() {
System.out.println("Wishes");
ArrayList<String> allWishes = new ArrayList<String>();
allWishes.add("aa");
allWishes.add("bb");
allWishes.add("cc");
allWishes.add("dd");
for (String i : allWishes) {
System.out.println("\t" + i);
}
}
public void displayDetail() {
DOB d = new DOB();
System.out.println("Name: " + getName());
System.out.println("Nationality: " + getNationality());
System.out.println("Date of birth: " + d.dateString(1, "Jan", 1000));
setHobbies();
setWishes();
}
}
class DOB {
public String dateString(int day, String month, int year) {
String dob = (day + " " + month + ", " + year);
return (dob);
}
}
class Test {
public static void main(String[] args) {
MyInfo my = new MyInfo();
my.displayDetail();
}
}
The very first value you don't want was the object ID, you simply can't return the values of an object. If you want to see the values then you have to override the toString()
method.
The second value was visible just because you returned the list. If you were using print()
inside your method then there was no need to return it.
Tips: You need to get a better understanding of getter and setter methods because you have messed up the entire concept in your code. Your blank constructor is not needed. Rather, you should initialize all the fields (class variables of Detail class) in the constructor.
答案2
得分: 0
在displayDetail()方法中移除System.out.println,如下所示,
setHobbies();
setWishes();
英文:
Remove System.out.println in displayDetail() method as below,
setHobbies();
setWishes();
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论