1
// Facility table attributes to be read in json format
String facilityName[], recApp[], recFacility[], sendApp[], 
       sendFacility[], enable[], doneness[], retryLimit[], 
       watchDelay[], retryDelay[], ackTimeout[], 
       keepConOpen[], sendTimeout[], cleanupDelay[], 
       host[], port[];

String facilityTableAttrs[][] = new String[][] { 
       facilityName, recApp, recFacility, sendApp, 
       sendFacility, enable, doneness, retryLimit, 
       watchDelay, retryDelay, ackTimeout, keepConOpen, 
       sendTimeout, cleanupDelay, host, port};

I have array of arrays called facilityTableAttrs declared as above.

I have 2 questions:

1) Is it possible to do the above array declaration in a single step ?

2) I wish to get the individual array names of these 1D array using something like:

for(i = 0; i < facilityTableAttrs.length; i++) {
    System.out.println(facilityTableAttrs[i].toString());
}

but it fails. How to get the individual array names as string?

1
  • variable names should start with a lower case character, so 'facilityName' instead of 'FacilityName'. Also it is more common to do 'String[][] facilityTableAttrs' than 'String facilityTableAttrs[][]' (which is also correct). Commented Jan 15, 2015 at 14:04

3 Answers 3

1

The first list of arrays you declare don't seem to be initialized anywhere.

As such they are null, and invoking toString on any of them will cause a NullPointerException to be thrown, hence "it fails".

By the way, invoking toString on an non-null array would actually print something similar to the Object.toString representation, which is not what you want (Arrays.toString(myArray) is your friend here).

You could initialize each and every single array and populate them optionally, before adding them to the main String[][] but I recommend you don't.

Instead, investigate the collections framework.

What you could use here is a Map<String, List<String>>.

Or better even, a custom object with properties such as List<String> facilityName, List<String> recApp, etc.

Finally, note the variable naming, which is camelBack according to code conventions.

Sign up to request clarification or add additional context in comments.

4 Comments

I can't populate them because the size is initially unknown, it will be picked up based on the values populated as json string by the user. I just need the array names like "FacilityName" "RecApp" etc.
@kingsmasher1 then I highly recommend using a custom object with a number of List<String> as properties, or worse but acceptable, a Map<String, List<String>> as indicated.
But in that case, i need to initialize each individual list, and that increases the code size, i can't have an array of list as well. Something like List<String> facilityTableAttrs[]=new List{List<String> FacilityName, List<String>RecApp is not allowed in Java
You will need to initialize any object you add objects to, be it an array or a List. You cannot initialize a List itself, it's an interface. You probably want ArrayList as your concrete class. As I already said, you're better of with a Map<String, List<String>> not a List<String>[].
0

This is not possible with arrays. You need to use map, like so:

Map<String, List<String>> myMap = new HashMap<String, List<String>>();

You need to choose correct data structure for your problem.

Arrays are used only for storing values, thay are not interestd in bounding names to them. Maps on the other hands are great with bounding names (keys that are unique) to any type of value.

1 Comment

Array os list is not allowed in Java. Initializing each attribute will icrease code size, which is the reason i wanted to use an array. Something like List<String> facilityTableAttrs[]=new List{List<String> FacilityName, List<String>RecApp is not allowed in Java
0

I propose to use a wrapper class:

public class Facility {
    private final String name;
    private final List<String> values;
    public Facility(String name) {
        this.name = name;
        this.values = new ArrayList<>(); 
    }
    public String getName() {
        return name;
    }
    public List<String> getValues() {
        return values;
    }
}

and then do:

Facility[] facilities = new Facility[] {
    new Facility("facility 1"),
    new Facility("facility 2"),
    new Facility("facility 3"),
    new Facility("facility 4"),
};
for(Facility facility : facilities) {
    System.out.println(facility.getName());
}

To add a value to a facility you'd do:

Facility facility = facilities.get(0);
facility.getValues().add("bla");

If you need to look up facilities by name, then use a Map instead of an array:

    ...
    // see createLookup method below
    Map<String, Facility> facilities = createLookup(
        new Facility("facility 1"),
        new Facility("facility 2"),
        new Facility("facility 3"),
        new Facility("facility 4"));

    // print names
    for(Facility facility : facilities.values()) {
        System.out.println(facility.getName());
    }

    // add a value
    Facility facility = facilities.get("facility 3");
    facility.getValues().add("bla");
}

private Map<String, Facility> createLookup(Facility.. facilities) {
    // use TreeMap to have sorted keys
    Map<String, Facility> lookup = new TreeMap<>();
    for(Facility facility : facilities) {
        lookup.put(facility.getName(), facility);
    }
    return lookup;
}

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.