0

So I am having trouble trying to loop through an array list but i dont want to use a println statement to print the elements from the array list. Is it possible if i could store all the elements into a local variable through each loop and then return the local variable when i call the method later on?e Here is my code:

public String displayProperties() {
        String property = null;
        for (int i = 0; i < properties.size(); i++) {
            property = properties.get(i);
        }
        return property;     
    }

9
  • no. you already have such a property, it's your list. In your current approach, you'll just return the last element, since you overwrite the value each iteration Commented Dec 3, 2019 at 10:39
  • @Stultuske, java.util.Properties extends java.util.Hashtable and hence has no predictable order for keys. So there is no such thing a "last element". Commented Dec 3, 2019 at 10:46
  • @Bobfflander, welcome. Could you clarify what you are trying to achieve. Commented Dec 3, 2019 at 10:48
  • What are you trying to accomplish? Your list properties already contains those elements and you can already get a certain property with your properties.get(i). What purpose do you had in mind wanting to store all values in properties in a separated local variable? Some additional questions: where are they coming from (if they come from a .properties files there are loads of libraries to directly insert each property in a separated variable through annotations); what do you want to use them for; etc. Commented Dec 3, 2019 at 10:48
  • So what I'm trying to do is in another part of my code, I want to call the method displayProperties() which prints out all the elements stored in the array list, however I do not want to use a print statement to display the array list. Commented Dec 3, 2019 at 10:57

2 Answers 2

3

You would have to introduce an instance variable in order to cache the result of your method (your local variable doesn't live past the current execution of the method).

And you'd also need to change the logic of that method, to append all the elements into a single String.

I also suggest adding some separator between the elements.

private String cachedProperties = null;
public String displayProperties() {
    if (cachedProperties == null) {
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < properties.size(); i++) {
            if (i > 0) {
                sb.append(',');
            }
            sb.append(properties.get(i));
        }
        cachedProperties = sb.toString();
    }   
    return cachedProperties;
}

Note that if the properties List may change, you have to reset your cache instance variable each time that happens.

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

1 Comment

Thank you this method worked! much appreciated.
1

May be modify the code to get the final string as below

property += properties.get(i);
property += " "

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.