【问题标题】:Create array of type base class创建类型基类的数组
【发布时间】:2020-01-18 18:25:22
【问题描述】:

标题不是很有启发性,请指教一个更好的。

可以在 oca-java-se O'Reilly 中找到该示例。

我有一个基类

public class Thing{
   public int thingOne;
   public int thingTwo;
}

主要

class MyMain{
public static void main (String[] args){
    Thing[] ta;

    // how do I initialize this array

    // is there a tricky way to access elements rather than ta[0] ??

  }
}

我很困惑。我想这个教训是如何创建一个基类类型的数组,初始化它并从中访问元素。

【问题讨论】:

标签: java arrays class


【解决方案1】:

最简单的做法是用给定的长度初始化数组对象,然后迭代以填充元素。一个简单的例子如下:

public class ArrayTest {

    public static class Thing  {

        private int thingOne;
         public int getThingOne() { return thingOne; }
         public void setThingOne(int thingOne) { this.thingOne = thingOne; }

         private int thingTwo;
         public int getThingTwo() { return thingTwo; }
         public void setThingTwo(int thingTwo) { this.thingTwo = thingTwo; }

         @Override
         public String toString() {
             return "Thing{" +
              "thingOne=" + thingOne +
              ", thingTwo=" + thingTwo +
              '}';
         }

    }

    public static void main(String... args) {
        Thing[] array = new Thing[10];

        for (int i = 0; i < array.length; i++) {
            Thing thing = new Thing();
            thing.setThingTwo(new Random().nextInt(10));
            thing.setThingTwo(new Random().nextInt(10));
            array[i] = thing;
        }

        Arrays.stream(array).forEach(System.out::println);
    }

}

【讨论】:

  • 谢谢。你的例子最有意义。我很感激你猜到我的意思。我提取了这个,
  • 您添加了getter 和setter 以及toString 真是太好了。后来我意识到我需要这些。我期待提供一个 add() 方法,因为这是一个数组。
  • @Kinf 谢谢!如果您认为我的回答涵盖了您的问题,请确保对其进行投票并使其被接受。
【解决方案2】:

首先,public class Thing(){ 是错误的。应该是public class Thing{

如何初始化这个数组

有很多方法可以初始化数组,例如

class Thing {
    public int thingOne;
    public int thingTwo;
}

class MyMain {
    public static void main(String[] args) {
        // First way
        Thing[] ta1 = new Thing[] {};

        // Also
        Thing[] ta2 = new Thing[] { new Thing(), new Thing() };

        // Using a loop
        Thing[] ta3 = new Thing[5];
        for (int i = 0; i < ta3.length; i++)
            ta3[i] = new Thing();
    }
}

有没有一种棘手的方式来访问元素而不是 ta[0]

如果你想使用它的索引值来访问一个元素,没有别的办法。但是,如果您确实需要使用索引值访问元素,则有一些方法,例如

import java.util.Arrays;

class Thing {
    public int thingOne;
    public int thingTwo;
}

public class MyMain {
    public static void main(String[] args) {
        Thing[] ta = new Thing[5];
        for (int i = 0; i < ta.length; i++)
            ta[i] = new Thing();

        // Accessing the elements without index value
        for (Thing t : ta) {
            System.out.println(t);
        }

        // Another way
        System.out.println(Arrays.toString(ta));

        // Also
        Arrays.stream(ta).forEach(System.out::println);
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-05-30
    • 2022-06-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多