1

Write a method public static ArrayList append(ArrayLista, ArrayList b) that appends one array list after another. For example, if a is 1 4 9 16 and b is 9 7 4 9 11 then append returns the array list 1 4 9 16 9 7 4 9 11. This is what I have done so far and I'm pretty sure I'm almost finished but I keep running into errors; mainly "cannot find symbol ArrayList" Can anyone help me?

import java.util.Arrays;

public class AlAppend { 

public static ArrayList<Integer> append(ArrayList<Integer> a, ArrayList<Integer> b) {
ArrayList<Integer> c = new ArrayList<Integer>();
c.addAll(a);
c.addAll(b);
return c;   
}

public static void main(String[] args) {

    // List 1
    ArrayList<Integer> array1 = new ArrayList<Integer>();
        array1.add(1);
        array1.add(4);
        array1.add(9);
        array1.add(16);
    System.out.println("List 1: " + array1);

    // List 2
    ArrayList<Integer> array2 = new ArrayList<Integer>();
        array2.add(9);
        array2.add(7);
        array2.add(4);
        array2.add(9);
        array2.add(11);
    System.out.println("List 2: " + array2);

    // Combined List
    ArrayList<Integer> array3 = append(array1, array2);
    System.out.println("Combined: " + array3);
}

}

1 Answer 1

1

Step 1)

Just change this :

c.add(a);
c.add(b);

to this :

c.addAll(a);
c.addAll(b);

PS : Next time you post question, please copy the error message, statement "keep getting into errors" is facepalm statement in programming community :).

Step 2)

Add line to the top :

import java.util.ArrayList;

Step 3)

Download netbeans, it is very user-friendly and it helps you solve errors by "tip" or even solving dependencies (like imports) for you.

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

1 Comment

I appreciate the help! I've got 10 errors left, most of them being; it cannot find the symbol ArrayList<Integer>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.