1

Painfully newbie question I know, but I'm very new to Java Programming.

I have 4 values, value1, value2, value3 and value4. I'd like a method that will return all 4 values, but I think I need an ArrayList to do so. All these values are in the same class, and the method will be within this class too.

I've never created one before, and Google doesn't really offer an answer I can understand at my very early level of Java understanding. Any help on how I create one for this?

(I assume I need an ArrayList, but I may be wrong)

3
  • @DoorknobofSnow: If you see the answer below. All mentioning same thing what he is thinking Commented Dec 17, 2013 at 13:12
  • @Sach No, mine doesn't. Why would you use an ArrayList when an array would work just fine? Commented Dec 17, 2013 at 13:13
  • @Sach No, I do not. I just provided a straight forward answer to what he was asking and provided a better solution afterwards. Returning an ArrayList here is most likely not the best approach. Commented Dec 17, 2013 at 13:13

4 Answers 4

2

You can just use a normal array. For example:

int[] getValues() {
    int value1 = 10, value2 = 20, value3 = -5, value4 = 3;
    int[] arr = new int[] {value1, value2, value3, value4};
    return arr;
}

No need to use an ArrayList when you know how many items you will always need.

It would be better to have an array, since they are native and therefore faster, less bloated, and much more simple.

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

3 Comments

Well, I'm not going to down vote because you are correct in that you don't have to use a List instance. And while, technically, arrays do provide a smaller footprint and accessing the elements is faster, they provide no type safety and lead to brittle code. Newbies should be worrying about good coding practices before performance.
@MadConan There is absolutely no reason to use an ArrayList when an array fits better. "they provide no type safety" is completely false... and I'm not sure how they lead to "brittle code"...
For primitive arrays, there isn't any issue with typing. However, the OP is asking a general question, and with Object arrays and generics, type safety cannot be provided using arrays. Unless there is a clear performance benefit to using arrays (only achieved through testing), Lists are preferred.
2

You can also declare and fill the array list at the same time:

List<String> list = new ArrayList<String>(){{
     add("A");
     add("B");
     add("C");
}};

2 Comments

Would this be in a new Class or in the current Class? (If the latter, where in the code would it need to be entered, really new to this!) Thanks!
Actually, what we are creating here is, an anonymous inner class with an instance initialiser block. Well, you should not worry about that. You can declare and use this in whichever class/method you want to. More technical details regarding this is in my blog -> techblog-dm.blogspot.in/2013/06/…
1
public List method() {
    ArrayList list = new ArrayList();
    list.add(item1);
    list.add(item2);
    list.add(item3);
    list.add(item4);
    return list;
}

But it most likely is better to have an inner wrapper class for that. Because that way you can give the various Objects meaningful names.

public DataClass method() {
    DataClass retVal = new DataClass(item1, item2, item3, item4);
}

private class DataClass {
    Object item1;
    Object item2;
    Object item3;
    Object item4;
    public DataClass(Object item1, Object item2, Object item3, Object item4) {
        this.item1 = item1;
        this.item2 = item2;
        this.item3 = item3;
        this.item4 = item4;
    }
}

1 Comment

doesn't have to be inner class, but I agree. Custom types > generic structures.
0

If you want to use java.util.List functionality you can simply use :

List<String> predefinedList = Arrays.asList(new String[] { "value1", "value2", "value3", "value4" });

and then in a method simply do return predefinedList;

Otherwise, if you only want to return an array of predefined values, then you can just use the following notation in your method: return String[] { "value1", "value2", "value3", "value4" }

3 Comments

Why not just return the String array?
Or Arrays.asList("value1", "value2", "value3", "value4");
you've missed the opening " on value 4

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.