简体   繁体   中英

how to initialize a static array of objects in java

Basically I want to create a data structure of values already known at compile time. In C I'd do it like this:

struct linetype { int id; char *descr; };

static struct linetype mylist[] = {
    { 1, "first" },
    { 2, "second" }
};

The only soultion I have found in Java involves creating the array at runtime:

public class Outer {

    public class LineType {
        int id;
        String descr;

        private LineType( int a, String b) {
          this.id = a;
          this.descr = b;
        }
    }

    LineType[] myList = { 
        new LineType( 1, "first" ),
        new LineType( 2, "second" ),
    };

This appears cumbersome and ineffective (when the structures get long and complex). Is there another way?

(NB: please disregard any syntax errors as this is only sample code created for this question. Also, I am aware a String is somethign else than a character pointer pointing into the data segment. However, the argument works with primitive data types as well).

You have to make LineType a static class:

public class Outer {

    public static class LineType {
        int id;
        String descr;

        private LineType( int a, String b) {
          this.id = a;
          this.descr = b;
        }
    }

    static LineType[] myList = { 
        new LineType( 1, "first" ),
        new LineType( 2, "second" ),
    };
}

除非有一些我没有得到的东西,否则这应该是这样简单:

Object[][] mylist = {{1, "first"}, {2, "second"}};

In Java, you can't create arrays at compile time (arrays are special type of objects). Either class load time using static blocks (or) runtime (as instance variable) you can create arrays.

Example static block:

class TestClass
{
     static {
     arr[0] = "Hi";     
     arr[1] = "Hello";     
     arr[2] = "How are you?"; 
    }
....
}

If you want to avoid using a new Object, you might use a Map instead of an array. Note that the first value (1, 2, etc) would always have to be unique though. See the Oracle documentation for Map .

private static Map<Integer, String> myMap = new TreeMap<Integer, String>();

static {
    myMap.put(1, "first");
    myMap.put(2, "second");
}

If the data structure is really really messy and complicated, and you really don't want to "clutter" you code with it, you could create it in a totally separate little program, and serialize/save it to disk. Your "real" program just deserializes/reads it.

Of course, this really obscures what is going on so I'd avoid this technique unless you really need it.

If the problem is only that the initial load speed of the app is slow, you can defer static initializers using the Holder Pattern

You can initialize them using static blocks in Java .

class Outer
{
  static 
  {
    // whatever code is needed for initialization goes here
  }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM