0

I need to make a method that can return 2 ArrayLists.

I have read some answers and came up with a few options:

  1. return Pair(arrList 1, arrList 2);

    This is the easier option, but I dont know if it will work, and I dont know how to extract the ArrayLists from the Pair in the method that calls this method

  2. Try making a class that holds both of those ArrayList. I don't know if this would work, but I think it should.

Does any one know if any of these options would work, or if there is any other options I could use?

Code-Apprentice
  • 81,660
  • 23
  • 145
  • 268
Eddie
  • 171
  • 5
  • 13
  • 1
    The `Pair` class should work. Just read the documentation in order to learn how to use it. – 4castle Jul 30 '17 at 03:23
  • @4castle I have started on the second option, do you think that would work too? – Eddie Jul 30 '17 at 03:25
  • 2
    The second option would also work, but then you're basically just reinventing the `Pair` class. – 4castle Jul 30 '17 at 03:26
  • 1
    Why do you have two ArrayLists? What do the two lists hold? Are the data related in some way? Are they parallel lists? If this is the case, then you can create a class which holds two related pieces of information and then create a single ArrayList that contains objects of this new class. – Code-Apprentice Jul 30 '17 at 04:17

6 Answers6

1

Create a class with property both heterogeneous list

public Class MyClass
{
   public List<> list1;
   public List<> list2;
}

Return the new object of the class from your method. If the lists are homogeneous, return a list holding both.
NOTE: both of your solutions are correct. It will work for sure, if you are doing it right.

Baum mit Augen
  • 49,044
  • 25
  • 144
  • 182
DropAndTrap
  • 1,538
  • 16
  • 24
0

Create a third arraylist, and put both arraylists in the third one and rwturn it.

Agam
  • 1,015
  • 2
  • 11
  • 21
  • public static void main(String[] args) { ArrayList> outer = new ArrayList>(); ArrayList inner = new ArrayList(); inner.add(100); inner.add(200); outer.add(inner); outer.add(inner); System.out.println(outer); } – Agam Jul 30 '17 at 03:28
  • @Agam You should edit your question so that you can format the code properly. – Code-Apprentice Jul 30 '17 at 04:18
0

You could use java.util.Map as return type;

Sample Code:

public Map<Integer, List> getLists() {
    Map<Integer, List> listMap = new HashMap<Integer, List>();
     List list1 = new ArrayList();
     List list2 = new ArrayList();
     // Fill your lists

     listMap.put(1, list1);     
     listMap.put(2, list2);

     return listMap;
}


// to get list from map
Map<Integer, List> lists = getLists();

List l1 = lists.get(1);
List l2 = lists.get(2);
rev_dihazum
  • 818
  • 1
  • 9
  • 19
0

I will go over how to implement each of the approaches you mentioned.

There is a Pair class in the Apache Common Lang API, which technically isn't part of the "standard" library. You can download it at http://commons.apache.org/proper/commons-lang/.

The documentation of Pair is here.

You can use it like this:

// in the method
public Pair<ArrayList, ArrayList> yourMethod() {
    ...
    return ImmutablePair.of(yourArrayList1, yourArrayList2);
}

// when you call the method
Pair<ArrayList, ArrayList> pair = yourMethod();
pair.getLeft(); // gets you the first array list
pair.getRight(); // gets you the second array list

You can also create your own Pair class, which is essentially what your second approach is trying to do.

Here's an example from this post.

public class Pair<K, V> {

    private final K element0;
    private final V element1;

    public static <K, V> Pair<K, V> createPair(K element0, V element1) {
        return new Pair<K, V>(element0, element1);
    }

    public Pair(K element0, V element1) {
        this.element0 = element0;
        this.element1 = element1;
    }

    public K getElement0() {
        return element0;
    }

    public V getElement1() {
        return element1;
    }

}

You can use it like this:

// in the method
public Pair<ArrayList, ArrayList> yourMethod() {
    ...
    return Pair.createPair(yourArrayList1, yourArrayList2);
}

// when you call the method
Pair<ArrayList, ArrayList> pair = yourMethod();
pair.getElement0(); // gets you the first array list
pair.getElement1(); // gets you the second array list
Sweeper
  • 213,210
  • 22
  • 193
  • 313
0

A third option is to create a class which holds the pair of elements in corresponding indexes of the two ArrayLists. For example, if one ArrayList contains names and the second ArrayList contains birthdays, then you can create a class which contains a name and a birthday. Now create a single ArrayList with instances of the new class.

This solution depends on the relationship between the data of the two ArrayLists. If the data are not as closely related as my example of names and birthdays, then this type of solution is probably not appropriate.

Code-Apprentice
  • 81,660
  • 23
  • 145
  • 268
0

I had to implement the same double ArrayList for Delta Debugging.

I used private variables with getters and setters and a constructor that initializes the ArrayLists with lengths in the constructor.

Here is my class (it is private because it is inside of a larger class where it is used):

private class DeltaSegments {
    private ArrayList<String> deltas;
    private ArrayList<String> nablas;

    DeltaSegments(int dSize, int nSize) {
        deltas = new ArrayList<String>(dSize);
        nablas = new ArrayList<String>(nSize);
    }

    public ArrayList<String> getDeltas() {
        return deltas;
    }

    public ArrayList<String> getNablas() {
        return nablas;
    }

    public void setDeltas(ArrayList<String> _deltas) {
        deltas = _deltas;
    }

    public void setNablas(ArrayList<String> _nablas) {
        nablas = _nablas;
    }
}

And here is how I instantiate it:

private DeltaSegments getDeltaSegments(String str, int segments) {

    ArrayList<String> deltas = new ArrayList<String>(segments);
    ArrayList<String> nablas = new ArrayList<String>(segments);

    // ... Add data to deltas/nablas here ... //

    DeltaSegments ds = new DeltaSegments(segments, segments);
    ds.setDeltas(deltas);
    ds.setNablas(nablas);
    return ds;
}

To access them later:

DeltaSegments ds = getDeltaSegments(failingLine, n);
ArrayList<String> deltas = ds.getDeltas();
ArrayList<String> nablas = ds.getNablas();

Hope this helps!

Ian
  • 1,746
  • 1
  • 15
  • 28