javaandroidlistarraylistmethod-invocation

Add Arraylists together created by a method call


I know that there are a few question already in this forum relating to my question, but none of them really seems the help me.

Since I am new to Coding I am still trying to figure out what exactly getClass() and getMethod() calls help me with.

What I want to accomplish:

// init:
List<Preview> listPreview;
List<Preview> listTemp;

// now create the Lists (from a Database)
listPreview = dbHelper.getPreview("Hero", "Axe");
listTemp = dbHelper.getPreview("Hero", "Beastmaster");

// now I want to add ListTemp to ListPreview
Class myClass = listPreview.getClass();
Method m = myClass.getDeclaredMethod("add", new Class[] {Object.class});
m.invoke(listTemp, 2);

The Problem:

Obviously this is not working right now, but I think the idea is pretty straight forward. I want to add listTemp to listPreview. The getDeclaredMethod is already considered a undeclared exception I do not really understand why.


Solution

  • If you want to add two list one after another just use this:

     listPreview.addAll(listTemp);