动态数组和数组方法

huangapple go评论60阅读模式
英文:

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(&quot;Name&quot;);
        n = new String[]{ &quot;ii&quot;, &quot;jj&quot; };
        for (int i = 0; i &lt; 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 = &quot;A&quot;;
}
public String getNationality() {
return nationality = &quot;abc&quot;;
}
public void setInfo(String name, String nationality) {
this.name = name;
this.nationality = nationality;
}
public static void setHobbies() {
hobby2 = new String[] { &quot;ii&quot;, &quot;jj&quot; };
System.out.println(&quot;Hobbies&quot;);
for (int i = 0; i &lt; hobby2.length; i++) {
System.out.println(&quot;\t&quot; + hobby2[i]);
}
}
public static void setWishes() {
System.out.println(&quot;Wishes&quot;);
ArrayList&lt;String&gt; allWishes = new ArrayList&lt;String&gt;();
allWishes.add(&quot;aa&quot;);
allWishes.add(&quot;bb&quot;);
allWishes.add(&quot;cc&quot;);
allWishes.add(&quot;dd&quot;);
for (String i : allWishes) {
System.out.println(&quot;\t&quot; + i);
}
}
public void displayDetail() {
DOB d = new DOB();
System.out.println(&quot;Name: &quot; + getName());
System.out.println(&quot;Nationality: &quot; + getNationality());
System.out.println(&quot;Date of birth: &quot; + d.dateString(1, &quot;Jan&quot;, 1000));
setHobbies();
setWishes();
}
}
class DOB {
public String dateString(int day, String month, int year) {
String dob = (day + &quot; &quot; + month + &quot;, &quot; + 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();

huangapple
  • 本文由 发表于 2020年10月1日 13:14:36
  • 转载请务必保留本文链接:https://go.coder-hub.com/64149453.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定