1

I'm doing a finance calculator in Java, and I need to be able to grab transaction info like Name, Amount, and Type. It needs to be dynamically sizable and able to hold the info. Basically I need a mix between an ArrayList and an Array of Objects but I can't really find anything that exists.. By looking at this question Creating an Arraylist of Objects, and the code below:

ArrayList<Matrices> list = new ArrayList<Matrices>();
list.add( new Matrices(1,1,10) );
list.add( new Matrices(1,2,20) );

it looks like I can create a new object each time that holds my data, so for my purposes it would look something like this

ArrayList<Payments> paymentList = new ArrayList<Payments>();
paymentList.add( new Payments(var1, var2, var3) ); 

Can I input variables into the ArrayList? Of course once I convert them all to strings (or could I keep them as String / Double / String since I'm technically adding an object to the ArrayList rather than a string value?

Community
  • 1
  • 1
pcort
  • 419
  • 1
  • 6
  • 19
  • 1
    I'm not sure I understand your question. If it is: can I add any kind of object into an ArrayList, the answer is yes. If it is: can a Payment object hold fields that are of type double in addition to other fields being of type String, the answer is of course also yes. – JB Nizet Dec 15 '12 at 21:57
  • Ok, yeah both of those questions are relevant to my initial question. My question is more a 'this is my thought process, will it work' type of thing. Another question now is, if I implement this, when I go to sort and search and all that, I'll have to basically run a loop that checks the paymentList ArrayList, and then within that check the Payments object correct? – pcort Dec 15 '12 at 22:36
  • No. You'll use Collections.sort() to sort the list. For searching, it depends on what you search, the size of the list and how often you search. A List is not necessarily the best datastructure to use. But without clear requirements, it's impossible to give a definitive answer. Read the [collections tutorial](http://docs.oracle.com/javase/tutorial/collections/). – JB Nizet Dec 15 '12 at 22:43

1 Answers1

0
paymentList.add( new paymentList(var1, var2, var3) ); 

means you are about to add a new Payments instance into the arraylist (paymentList). regarding the type of var1,var2 and var3 (String or double). it depends on how did you define the constructor of your Payments class.

as long as you have an instance of Payments, you could add to that arrayList.

Kent
  • 189,393
  • 32
  • 233
  • 301