如何将List更改为List(Array)?

时间:2017-01-12 21:50:03

标签: java playframework ebean

我想将模型分配给List(数组(模型)) 代替 列表(型号)ex:

final List<String[]> pageRolesOR = Arrays.asList(new String[]{"Role1"}, new String[]{"Role2"});

final List<String[]> pageRolesAND = Collections.singletonList(new String[]{"Role1", "Role2"});

=&GT;以下示例我可以获得List(Model)但是,如何将下面的模型更改为List(Array(Models))?

List<Page_Roles> pr = Page_Roles.find.where().eq("page_id","4").findList();

提前谢谢。

2 个答案:

答案 0 :(得分:0)

import java.lang.reflect.Array;
import java.util.LinkedList;

public class ArrayToList {
    public static void main(String[] args) {
        LinkedList list = new LinkedList();
        for(int i = 0; i < 10; i++) {
            list.add(i+""); //Taking String objects in the list
        }
        Array[] arr = (Array[])list.toArray();  //this becomes array of Strings
        LinkedList listFinal = new LinkedList();
        for(int i = 0; i < 10; i++) {
            list.add(arr); //simply adding same array into the whole LinkedList 
                            //you can add different arrays.
            //So the List(String) --> List(Array(String))
        }

        String[] strArray = new String[10];
        for(int j = 0; j< 10; j++){
            strArray[j] = j+"";   //creating a String array
        }
        LinkedList list2 = new LinkedList();
        for(int i = 0; i < 10; i++) {
            list2.add(strArray[i]); //Taking String array in the list
        }
        Array[] arr2 = (Array[])list2.toArray();
        LinkedList listFinal2 = new LinkedList();
        for(int i = 0; i < 10; i++) {
            listFinal2.add(arr2); //simply adding same array of array into the whole LinkedList 
            //So the List(String[]) --> List(Array(String[]))
        }
    }
}

答案 1 :(得分:0)

我使用与(Maninder Singh)解决方案相同的方法,除了通过List更改LinkedList以将Models.roles转换为List的这个孩子,但仅用于AND而不是OR ..

OR转换有点困难...... ^^反正谢谢,非常感谢

// Page Statistics Roles as List
List<Page_Roles> pr = Page_Roles.find.where().eq("page_id","4").findList();


// OR
List<String[]> pageRolesOR =  pr.stream().map(Page_Roles::toArray).collect(Collectors.toList());


// AND
List<String> list = new ArrayList<String>();
for(Page_Roles tt:pr) {list.add(tt.role.getName());}  // Getting List of Strings ( Roles.Names )

String[] array = new String[list.size()];
list.toArray(array);                                  // Convert List to Array

List<String[]> pageRolesAND = new ArrayList<String[]>();
pageRolesAND.add(array);                              //Adding Array to List



/*
 * Examples of Directe Puting Values  
final List<String[]> pageRolesOR = Arrays.asList(new String[]{"Rôle Administration"}, new String[]{"Rôle Qualité"});
final List<String[]> pageRolesAND = Collections.singletonList(new String[]{"Rôle Administration", "Rôle Qualité"});
*/