Beppi's
Beppi's

Reputation: 2119

Define a table of constants using arrays

In Java, I would like to create some kind of table of information, made of arrays of final static constants. I would like to initialize them at declaration time. I tried with array, ArrayList, HashMap, but I cannot find a syntax that allows me to write something similar to this.

public static final (something) names = {
        { "Albert Einstein", number_1, number_2 } ,
        { "Isaac Newton", number_3, number_4 } ,
        { "Pitagora", number_5, number_6 } ,
};

As you can see is an array of arrays, and creates an equivalent to a DB table. The columns of this sort of table are made of different types (although all data in the first place will always be strings, in second place integers, and so on)

What shall I put in place of (something)?

Upvotes: 0

Views: 517

Answers (6)

Bohemian
Bohemian

Reputation: 425003

If you don't mind a lack of strong typing, ie using Object, you can simply use an array of array of Object.

This compiles:

public static final Object [][] names = {
    { "Albert Einstein", number_1, number_2 } ,
    { "Isaac Newton", number_3, number_4 } ,
    { "Pitagora", number_5, number_6 } ,
};

Upvotes: 1

Sergey Kalinichenko
Sergey Kalinichenko

Reputation: 726549

It appears that you are looking for a custom class:

public class Scientist {
    private String name;
    private int numInventions;
    private int numStudents;
    public Scientist(String name, int numInventions, int numStudents) {
        this.name = name;
        this.numInventions = numInventions;
        this.numStudents = numStudents;
    }
    public String getName() {return name;}
    public int getNumInventions() {return numInventions;}
    public int getNumStudents() {return numStudents;}
}

Now you can initialize your array as follows:

Scientist[] scientists = new Scientist[] {
    new Scientist("Albert Einstein", number_1, number_2),
    new Scientist("Isaac Newton", number_3, number_4),
    new Scientist("Pitagora", number_5, number_6)
};

Upvotes: 1

Dororo
Dororo

Reputation: 3440

It would probably be better to do it in the static initialiser e.g.

public static final List<Something> names;

static {
    names = new List<Something>();
    // init here below using .add()
    names.add(Something); // something would contain the 3 fields you're grouping together
}

Upvotes: 0

Vincent van der Weele
Vincent van der Weele

Reputation: 13177

Arrays represent a list of the same type of data. Therefore,

 { "Albert Einstein", number_1, number_2 }

should not be an array. In fact, it cannot be anything standard in Java, because this is something specific to your application.

Make some class like

public class MyData {
    public final String name;
    public final int number1;
    public final int number2;

    public MyData(String name, int number1, int number2) {
        this.name = name;
        this.number1 = number1;
        this.number2 = number2;
    }
}

And declare

public static final MyData[] names = new MyData[] {
    new MyData("Albert Einstein", number_1, number_2) ,
    new MyData("Isaac Newton", number_3, number_4),
    new MyData("Pitagora", number_5, number_6)
};

Upvotes: 4

SJuan76
SJuan76

Reputation: 24885

1) Encapsulate. Create a Class that gets together a set of attributes (the name and its related attributes)

2) Create an array [] or a Collection (use a subclass) to hold values (instances) of that class

Upvotes: 0

Suresh Atta
Suresh Atta

Reputation: 121998

Try

 public static final String[][] names = { namesarray1, namesarray2 };

Upvotes: 0

Related Questions