Home > Mobile >  Android how the two List<Object> Set the same subscript value addition to generate a new List
Android how the two List<Object> Set the same subscript value addition to generate a new List

Time:11-21

For example List List1=Arrays. AsList (1.2, 2.3, 3,4.7, 5);
List List2=Arrays. AsList (1,2,3.6, 4, 5);
How to combine two lists into List ResultList=Arrays. AsList (2.2, 4.3, 6.6, 8.7, 10);

CodePudding user response:

If there is no specific method, oneself write cycle together

CodePudding user response:

Write their own code, intersection, and set a lot of online

CodePudding user response:

List List1=Arrays. AsList (1.2, 2.3, 3,4.7, 5);
List List2=Arrays. AsList (1,2,3.6, 4, 5);
List1addAll (list2);

CodePudding user response:

new reference 4 floor geithner reply:
List List1=Arrays. AsList (1.2, 2.3, 3,4.7, 5);
List List2=Arrays. AsList (1,2,3.6, 4, 5);
List1addAll (list2);

Subjects are wrong, he add up the value of the two lists the same position,

CodePudding user response:

Stupid method, with the size small to do benchmark, the for loop index after the same value addition deposit to the new List

CodePudding user response:

 
List List1=Arrays. AsList (1.2, 2.3, 3,4.7, 5);
List List2=Arrays. AsList (1,2,3.6, 4, 5);
List List3=new ArrayList<> (a);
For (int I=0; i If (I & lt; Math. Min (list1. The size (), list2. The size ())) {
List3. Add ((int) list1. Get (I) + (int) list2. Get (I));
} else {
List3. Add ((list1. The size () & gt; List2. The size ())? List1. Get (I) : list2. Get (I));
}
}
  • Related