1

I think it can be explained better with example: I have arrayLists by names sname,stime,snumber,etc., each carrying different values and a dynamic string 'dString' that is a concatenation of "s" and a variable VR that carries (name, time, number, etc.). Bbased on the value of VR i get from a loop, i'd like to use respective arraylist in a method. How can i use dString to refer to respective arraylist?

ArrayList<String> sname = new ArrayList<>();
ArrayList<String> stime= new ArrayList<>();
ArrayList<String> snumber = new ArrayList<>();

String[] VR = {"name","time","number"};

for(String str:VR) {
    String dString = "s"+str;
    String temp= dString.get(2); //How to get this?
}

(This is just as an example. the arraylists aren't empty. it's a long program so i haven't included it).

6
  • 1
    That is not going to work and is not supposed to. Use a Map (key-value store) instead. You cannot access local variables by name dynamically. Commented May 3, 2019 at 10:45
  • 1
    And unrelated: read about java naming conventions. Using hungarian notation like sname to say "it is a string" is so 1990. We dont do that in Java. An appropriate name would be "names", or "timestamps". And hint: dont store time as string. Use one of the many built-in classes that represent time information. If you only use raw strings, why bother using a statically compiled language anyway?! Commented May 3, 2019 at 10:49
  • 1
    Just a comment given to your I'm trying to reduce the program size as much as possible and cut down code and variables wherever possible ... you are going down a very dangerous path there! Large programs, with big databases are probably important. Chances are that you or other people will be working with that source code for many weeks, months, years to come. When you sacrifice readability for performance ... chances are that you get neither of that. Seriously: never assume that you can improve Java performance by writing "clever" java source code! Commented May 3, 2019 at 11:52
  • 2
    To the contrary: Java performance depends on the Just in time compiler being able to do its job. And guess what: the JIT works best with small methods, and code that looks like 99% of the code that other people write. As soon as you try to write "clever" java source code, you make it potentially harder for the JIT to properly optimize your bytecode, when turning it into machine code! Thus: when you really have performance issues, then use a profiler to measure where exactly your code spends most CPU cycles. Commented May 3, 2019 at 11:54
  • @GhostCat thank you. I’ll keep that in mind and switch back to classes like stephan Hogenboom also suggested. The code in question and the variables are all for example. The variables and code i’m working with are different. But thank u for the advice. I’ll avoid using hungarian notations in future. Also, thank you for calling it ‘clever ‘ ^_^ Commented May 7, 2019 at 2:48

1 Answer 1

2

The answer is: you should be using a Map.

That is the data structure that Java offers you to map a value (for example a List of Strings) to a key (for example: a String).

In other words: don't invent your own "dictionary", when the language already offers that concept to you.

Beyond that, the real answer would be to go "full" OOP. Meaning: you shouldn't have three different lists that together describe some object (linked by a common index). Instead you rather create a class that has name, title, and number fields. So that you only hold one list of such objects.

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

7 Comments

But in maps, i can only input dString as key. i cannot include respective value. it'll take as many maps as arraylists.
@Dante you should make an Object having the fields name, time and number. You can than store the objects in the map by using the map.put(number, object). You can than retrieve the object by getting the number on the map
@StephanHogenboom, I've used an ArrayList<ArrayList<String>> 'aalist' to store the arraylists' names in, and in hashmap, i used map.put(dString, aalist.get(aalist.getIndexOf(dString))); Will this work or will it be inefficient?
@Dante i would consider that inefficient to be honest. Can't you make a class VR { private String sname, private String stime, private String snumber } + getters and setters off course. And make a Map<String, VR> ?
@StephanHogenboom it's a really long program and it has a database to boot with. so I'm trying to reduce the program size as much as possible and cut down code and variables wherever possible. will using the above said method increase memory consumption?
|

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.