0

So I have ArrayList of ArrayLists and I'm using for example outer.get(0) in order to get the first ArrayList but I'm only getting the object reference I'm new to java please help me. Here is my code if you are interested

public class MyRecursiveTask extends RecursiveTask<List<Integer>> {

    private List<Integer> numbers;

    public MyRecursiveTask(List<Integer> numbers) {
        this.numbers = numbers;
    }

    protected List<Integer> compute() {

        //if work is above threshold, break tasks up into smaller tasks
        if(this.numbers.size() > 2) {
            System.out.println("Splitting workLoad : " + this.numbers.size());

            List<MyRecursiveTask> subtasks = new ArrayList<MyRecursiveTask>();
            subtasks.addAll(createSubtasks());

            for(MyRecursiveTask subtask : subtasks){
                subtask.fork();
            }

            for(MyRecursiveTask subtask : subtasks) {
                subtask.join();
            }
            System.out.println(subtasks.get(0));
            subtasks.get(1);

            return numbers;

        } else {
            System.out.println("Doing workLoad myself: " + this.numbers.size());
            bubbleSort(numbers);
        }
        return numbers;
    }

    private List<MyRecursiveTask> createSubtasks() {
        List<MyRecursiveTask> subtasks = new ArrayList<MyRecursiveTask>();
        List<Integer> list1 = numbers.subList(0,numbers.size()/2);
        List<Integer> list2 = numbers.subList(numbers.size()/2, numbers.size());

        MyRecursiveTask subtask1 = new MyRecursiveTask(list1);
        MyRecursiveTask subtask2 = new MyRecursiveTask(list2);

        subtasks.add(subtask1);
        subtasks.add(subtask2);

        return subtasks;
    }
  • can you please show the code with some input and output example – Ryuzaki L Oct 02 '19 at 20:24
  • if `outer.get(0)` returns an `ArrayList`, you could call `.get(0)` on that, too, like this: `outer.get(0).get(0)` – Kaan Oct 02 '19 at 20:33
  • the problem is that it doesn't return ArrayList but an reference – Яни Георгиев Oct 02 '19 at 20:46
  • By _"I'm only getting the object reference"_, do you mean that `System.out.println(subtasks.get(0));` is printing out sometihing like "ArrayList@2f92e0f4”? That's normal. You **are** getting an ArrayList back. You can't just print an ArrayList (or any object that lacks a toString() method) with System.out.println. See [this question](https://stackoverflow.com/questions/29140402/how-do-i-print-my-java-object-without-getting-sometype2f92e0f4) for more details. – Jordan Oct 02 '19 at 20:48
  • In the ```subtasks``` list you're adding an object of ```MyRecursiveTask``` which is not a list, so when you get ```subtasks.get(0)```, you need to store that in an object reference of ```MyRecursiveTask``` and then you need to call the list in that object with the dot operator. – YouKnowWhoIAm Oct 02 '19 at 21:05
  • @ЯниГеоргиев@Jordan Yes we can print an ArrayList with SOP – YouKnowWhoIAm Oct 02 '19 at 21:12
  • then what should my toStrinng function be – Яни Георгиев Oct 02 '19 at 21:14

2 Answers2

0

You're storing a list objects, so when you want to access the first element in that list, you'll get the reference to object that you stored in your list, and so inorder to access the list inside of that of object you need to use the object.value property and view the list.

To get the contents of every list in the main list, you do something similar to this,

for(MyRecursiveTask sub : subtasks) 
    System.out.println(sub.numbers);

EDIT #1 I have tested this program in an online compiler, and it works fine, made few changes to your code though. (Changes were made so that unnecessary things would be removed and not because they cause error).

public class MyClass {
    public static void main(String args[]) {
        //Driver to test your code
        ArrayList<Integer> arr = new ArrayList<>();
        arr.add(1);arr.add(2);arr.add(3);
        MyRecursiveTask e = new MyRecursiveTask(arr);
        e.createSubtasks();
    }
}

class MyRecursiveTask {

    private List<Integer> numbers;

    public MyRecursiveTask(List<Integer> numbers) {
        this.numbers = numbers;
    }

    public List<MyRecursiveTask> createSubtasks() {
        List<MyRecursiveTask> subtasks = new ArrayList<MyRecursiveTask>();
        List<Integer> list1 = numbers.subList(0,numbers.size()/2);
        List<Integer> list2 = numbers.subList(numbers.size()/2, numbers.size());

        MyRecursiveTask subtask1 = new MyRecursiveTask(list1);
        MyRecursiveTask subtask2 = new MyRecursiveTask(list2);

        subtasks.add(subtask1);
        subtasks.add(subtask2);
        //Below is the line that does all the work        
        for(MyRecursiveTask sub : subtasks) System.out.println(sub.numbers);

        return subtasks;
    }
}

OUTPUT

[1]
[2, 3]
YouKnowWhoIAm
  • 334
  • 1
  • 12
0

You need to add a getter method to get the list from the MyRecursiveTask object reference,

public class MyRecursiveTask extends RecursiveTask<List<Integer>> {

    private List<Integer> numbers;

    public List<Integer> getNumbers() {
        return numbers;
    }

    ...
}

You can get the value in following way,

List<MyRecursiveTask> outer = ...
MyRecursiveTask object = outer.get(0);
List<Integer> value = object.getNumbers(); // This is the value
Roaim
  • 2,298
  • 2
  • 11
  • 23