10

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).

1
  • 3
    Lots of coding atrocities were committed in the name of efficiency :) Commented Nov 12, 2012 at 19:01

6 Answers 6

11

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" ),
    };
}
Sign up to request clarification or add additional context in comments.

1 Comment

good idea. that way the runtime initialzation will happen only once, not with every instance creation.
2

Unless there is something I'm not getting, this should be as simple as:

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

update 2024

Somebody upvoted this very old answer. Java has advanced since 2012 and the answer now would look more like:

record Linetype(int id, String descr){};

static List<Linetype> mylist = List.of(
  new Linetype(1, "first"),
  new Linetype(2, "second)
);

2 Comments

You're right, this will work. Of course, access will be via indices, and you loose all type information, so this can only be a workaround in very simple cases (when the performance benefit is negligible).
You can iterate an array. Just curious, have you looked at enums?
1

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?"; 
    }
....
}

Comments

1

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");
}

1 Comment

a bit unrelated, but good to know, since I might need associative array access.
0

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

Comments

-1

You can initialize them using static blocks in Java.

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.