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

  • 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 – Stultuske Dec 03 '19 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". – Ambro-r Dec 03 '19 at 10:46
  • @Bobfflander, welcome. Could you clarify what you are trying to achieve. – Ambro-r Dec 03 '19 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. – Kevin Cruijssen Dec 03 '19 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. – Bobfflander Dec 03 '19 at 10:57
  • I want to store it in a local variable so that when I call the method I can use the local variable which contains the elements in an array list. Properties is from another file that contains information which I import at another class I have defined – Bobfflander Dec 03 '19 at 11:00
  • This method will return your last property value, all the time. – Antal Attila Dec 03 '19 at 11:04
  • @Ambro-r maybe not fixed, but the way he proposes will result in the laster element iterated upon. Besides, we're talking about an ArrayList, not about java.util.Properties – Stultuske Dec 03 '19 at 11:30
  • @Stultuske, cool. The ArrayList portion was a bit unclear. In which case I'd just use a stream. – Ambro-r Dec 05 '19 at 10:30

2 Answers2

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.

Eran
  • 387,369
  • 54
  • 702
  • 768
1

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

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