0

So basically Im reading a file , each line of this file contains description for from 3 to 5 instances of two types of objects with 2 to 4 being first type of object and last one being another type . The file can contain up to 500 lines . I am going to reuse each object from 1 to unknown but on scale of hundrets of times and I need to track their status .

While I can all the data into an array and use only 5 objects changing their values constantly that will make it rather hard to track status of each combination of their parameters .

What I want to is to create my instances with names such as FromLine1Obj1 , FromLine10Obj3 .

I failed to mention the fact that each set of objects from a single line should also create a new thread and this set is proccesed in that thread .

4
  • Which way classes with names should be created? Commented Aug 29, 2012 at 14:31
  • something like Contract Line(linenumber)Obj1 = new Contract(); Commented Aug 29, 2012 at 15:01
  • and then Line(linenumber).type = "Sell" Commented Aug 29, 2012 at 15:04
  • where linenumber is the number of line from which object description was taken Commented Aug 29, 2012 at 15:05

4 Answers 4

4

You need to use a Map.

Map<String, MyObject> map = new ....

map.put("FromLine1Obj1", new MyObject());

MyObject mo = map.get("FromLine1Obj1");
Sign up to request clarification or add additional context in comments.

3 Comments

Thank you for the best answer about Map , but it seems to be just a set of special arrays with additional functions .
HashMap does use an array but TreeMap doesn't. What is your doubt?
I need each line analyzed in a paralell thread . And the number of threads is based on amount of lines in my file .
1

If you read the file once and it will not change after all - you may want to do some code generation, like http://cglib.sourceforge.net/

4 Comments

The file is basicly a list of insturctions loaded at program start up .
Does it change from compilation to compilation?
Yes the file changes from compilation to compilation , it is also changeable after compilation.
then you will need to use Map, as described in another comments.
0

Could you not use a map with the name as the key?

Comments

0

You can store your parsing result in a map, with as key the name you want and as value the created instance, that's the clean way around name indirection in Java.

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.