Java: How to insert elements from one array to another in specific sequence(homework) -
i have problem arrays. update:
need insert elements array array b, staring uneven indexes , ones afterwards. example: a[0] should become b[10], a1=b[0],a[2]=b[11] etc. have updated code (thank comments, tips , suggestions!).
(i=0; i<10; i++){ if (i%2==1) b[c-1]=a[i]; c++; } (i=0; i<10; i++){ if (i%2==0) b[c]=a[i]; c++; } now fill array b, every other element. see picture: right output this
how b array fill in right?
sorry, if sounds stupid, started learn programming.
this not elegant or efficient solution, think it's simple enough understand , follow.
public static void main(string[] args) { double[] = {0,1,2,3,4,5,6,7,8,9}; double[] b = new double[a.length]; double[] temparray = new double[a.length]; int tempcounter = 0; int indexcounter = 0; for(int = 0; i<a.length; i++){ if (a[i] % 2 == 0){ // temparray[tempcounter] = a[i]; tempcounter++; }else{ b[indexcounter] = a[i]; indexcounter++; } } for(int = 0; i<tempcounter; i++){ b[indexcounter] = temparray[i]; indexcounter++; } }
Comments
Post a Comment