Without more detail, it's difficult to give specific advice. But possibilities include:
Put related functions into a single class and make the data member variables. Like to take your "find and minipulate two smallest":
class ManipulateLeast2
{
List<int> list;
int firstSmallest;
int secondSmallest;
public ManipulateLeast2(List<int> list)
{
this.list=list;
}
public void findTwoSmallest()
{
.. scan list and populate firstSmallest and secondSmallest ...
}
public void processTwoSmallest()
{
... process firstSmallest and secondSmallest and do whatever ...
}
}
If that's awkward, another possibility is to create a class to hold the values you need and return that. Like:
public class Smallest
{
int first;
int second;
public Smallest(int first, int second)
{
this.first=first;
this.second=second;
}
}
...
public Smallest findSmallest(List list)
{
... find two smallest and put in first and second ...
// Create the object and return it
return new Smallest(first, second);
}
I think this is much superior to faking out a pass-by-reference. But if you really insist, it could be done by putting the value in a wrapper, like this:
public StringWrapper
{
public String s;
}
...
// callee
void getTwoStrings(StringWrapper sw1, StringWrapper sw2)
{
sw1.s="Hello";
sw2.s="Goodbye";
}
// caller
StringWrapper sw1=new StringWrapper();
StringWrapper sw2=new StringWrapper();
getTwoStrings(sw1, sw2);
System.out.println("First is "+sw1.s);
System.out.println("Second is "+sw2.s);
To make this clean you'd need a separate wrapper for each class. I suppose you could make an ObjectWrapper to make it generic and then cast things, but this is getting quite messy.
I think you're much better off to think of how your data relates and move it into logical classes.