I have two ArrayList
s :
ArrayList a = [1,2,3,4,5,6,7,8,9,10,11.......100]
ArrayList b = [a1,a2,a3,a4,a5,a6,a7,a8,a9,a10.......a100]
remove the every 5th and 6th elements of ArrayList
a like 5,6 11,12 and so on and to the ArrayList
b.
and also remove every 6th and 7th item of ArrayList
b like a6,a7,a13,a14 so on and add it to ArrayList
a.
so my output ArrayList
s would be.
ArrayList a = [1,2,3,4,a6,a7,7,8,9,10,a13,a14,13,14,15,16,a20,a21 and so on]
ArrayList b = [a1,a2,a3,a4,a5,5,6,a8,a9,a10,a11,a12,11,12 ,a15,a16,a17,a18,a19,17,18and so on]
so how can I achieve this?
I tried it but not in an exact way.
int highAdded = 0;
int normalAdded = 0;
for (Iterator<BaseItem> iterator = mItems.iterator(); iterator.hasNext(); ) {
BaseItem itemtype = iterator.next();
if (itemtype.isHighlightPost()) {
highAdded++;
if (highAdded == 5) {
normal.add(itemtype);
} else if (highAdded == 6) {
normal.add(itemtype);
highAdded = 0;
} else {
highlighted.add(itemtype);
}
} else {
normalAdded++;
if (normalAdded == 6) {
highlighted.add(itemtype);
} else if (normalAdded == 7) {
highlighted.add(itemtype);
normalAdded = 0;
} else {
normal.add(itemtype);
}
}
}
Thanks
this should do the job, note that here there is no removal, but it does replacements. Ensure that N is set so that no list sizes are exceeded.
/** Multiplier for a-list index */
private final static int M_A = 6;
/** Multiplier for b-list index */
private final static int M_B = 7;
{
// ...
final int n = Math.min( a.size() / M_A, b.size() / M_B );
for ( int i = 1; i <= n; i++ ) {
exchange( a, b, i * M_A - 2, i * M_B - 2 );
exchange( a, b, i * M_A - 1, i * M_B - 1 );
}
// ...
}
private <T> void exchange( List<T> a, List<T> b, int aIndex, int bIndex ) {
T elem1 = a.get( aIndex );
a.set( aIndex, b.get( bIndex ) );
b.set( bIndex, elem1 );
}
(edited to match 'specification' for index counters) (edit 2, added also computation of n)
you can done this through set() method of arraylist
public void arraylistproblem()
{
ArrayList<String> a=new ArrayList<>();
for(int i=1;i<=100;i++)
{
a.add(""+i);
}
ArrayList<String> b=new ArrayList<>();
for(int j=1;j<=100;j++)
{
b.add("a"+j);
}
System.out.print("Arraylist a = ");
for(int i1=0;i1<a.size();i1++)
{
System.out.print(a.get(i1)+",");
}
System.out.print("Arraylist b = ");
for(int i1=0;i1<b.size();i1++)
{
System.out.print(b.get(i1) + ",");
}
int aIndex=4;
int bIndex=5;
for(int i=0;i<a.size();i++)
{
if(aIndex>=a.size() || bIndex>=b.size())
break;
String aTemp1=a.get(aIndex);
String bTemp1=b.get(bIndex);
/**
* swap the values .. 5th of a-arraylist and 6th of b-arraylist
*/
a.set(aIndex++, bTemp1);
b.set(bIndex++, aTemp1);
String aTemp2=a.get(aIndex);
String bTemp2=b.get(bIndex);
/**
* swap the values .. 6th of a-arraylist and 7th of b-arraylist
*/
a.set(aIndex, bTemp2);
b.set(bIndex, aTemp2);
aIndex=aIndex+5;
bIndex=bIndex+6;
}
System.out.print("Arraylist a = ");
for(int i1=0;i1<a.size();i1++)
{
System.out.print(a.get(i1)+",");
}
System.out.print("Arraylist b = ");
for(int i1=0;i1<b.size();i1++)
{
System.out.print(b.get(i1)+",");
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.