This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes Returning ArrayList from Method Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Returning ArrayList from Method" Watch "Returning ArrayList from Method" New topic

Returning ArrayList from Method

James Dudley

Joined: May 20, 2011
Posts: 17
I have been trying to condense some duplicates of code and hit a barrier

I have a class that reads information from a database. I had 4 methods to return an array of 4 different items from database.
I got told about merging them into one method and create a ArrayList of Objects

Here is the new method

So I have returned the Array of Objects (I believe)

My issue now is reading that back
In the Class I am reading it to I have called the Class

I have defined a new ArrayList<ScoreButtonValues> called scoreButtonValue

I have tried this

and then do the normal

but the ScoreButtonValues values cannot be accessed

Can someone point me in the right direction

Ralph Cook
Ranch Hand

Joined: May 29, 2005
Posts: 479
Well, there's almost enough information here.

Do you expect multiple records back from the database query? Your loop is going to create one ScoreButtonValues object per record returned. But your text talks about getting back four values, making me wonder if you expect only one record back from the query.

Your text also makes me think you want to use an array list to return 4 values from your function; but your ArrayList is of ScoreButtonValues, and each object of that type evidently has 4 values in it.

If your method is supposed to return one object with 4 values in it, you don't need the ArrayList; just have the method return a ScoreButtonValues object, then access the values within it with "getter" methods, for example public String getArrowValue(), etc.

If you're trying to return multiple objects, each of which has 4 values, then this is closer to correct. And in fact, in that case, I don't know what your problem is because you didn't tell us. You say the "values cannot be accessed", but you don't tell us what you mean. Are you calling methods that return null? Do you not know how to define methods to return the values? Does the loop blow up before you get that far, and, if so, what kind of error do you get?

James Dudley

Joined: May 20, 2011
Posts: 17

The table will output 10 rows. I created 4 methods to bring back a single column for each of the 10 rows and add them to an ArrayList. I was then using each ArrayList in for loops to create buttons
I was told I could do this within a single method by adding each of the 4 columns into an object and then adding them into an ArrayList

Here is the Object Class code

So as far as I understand the code in the first forum thread will create 10 objects of the above into an arraylist

What I trying to do then is while loop though the ArrayList in the main class and create 10 buttons with names as scoreValueDisplayName, background as scoreValuesColourBackground and forground as scoreValuesColourForeground

Does that help
Ralph Cook
Ranch Hand

Joined: May 29, 2005
Posts: 479
Ok. I'm going to guess that all this OO and Java stuff is very new to you. My apologies if this is too elementary, but something about the way you said "I've been told" leads me to believe this is what you're looking for.

The ScoreButtonValues object you gave us needs some additions. The traditional, better-software-engineering way to do it is something like this:

I have made your four variables "private", meaning they cannot be accessed by code outside the class, and I've written two of the "getter methods" that you can use to retrieve values. So if you have an object x of this type, you can use "x.getScoreValue()" to return the int score value, etc.

You could also declare the variables public, and then access each variable using the object reference (such as x) followed by a period followed by the variable name ("x.scoreValue"). I don't recommend this, because it gives you less flexibility than the getter method method.

One more idea for you to ponder. You are putting these vars in an object of type "ScoreButtonValues". You could consider shortening their names to things like "value", "displayName", etc. -- then you would access them with constructs like "x.getValue()" or "x.value". Since they are already in an object of a type that indicates what value it is, I invite you to consider whether the brevity makes it more or less clear.

I hope this answered your question.

James Dudley

Joined: May 20, 2011
Posts: 17
Thanks for your time, yes I relative new to this java thing

Changed the class to make the values private but not 100% sure why this is better then having them public

also not sure what your last paragraph was about if you could explain further I would be grateful
I agree. Here's the link:
subject: Returning ArrayList from Method
Similar Threads
Adding To An ArrayList
Global Array not working
Problem accessing Database from JSP
Arraylist with Objects
ArrayList Question