java - Loading an Array from another Array -
it doesn't seem work properly
'
public class junk { public static void main(string[] args){ int[] arr = {1,0,1,1,0,1,0}; int[] newarr = null; for(int = 0; < arr.length; i++){ if(ispoweroftwo(i)) { newarr[i] = 9; } else{ newarr[i] = arr[i]; } } (int = 0; < newarr.length; i++) { system.out.print(newarr[i] + ", "); } } private static boolean ispoweroftwo(int i) { if (i <= 0) { throw new illegalargumentexception("number: " + i); } if ((i & -i) == i) { return true; } return false; }
}
'
an array 10011010 of this, , want 9 9 1 9 0 0 1 9 1 0 1 0
so power of 2 indices should filled 9's getting right?
how load data of bits in array or arraylist loop , indices 9... want 1st,2nd, 4th , 8th indices...
_ _ 1 _ 0 0 1 _ 1 0 1 0
issue in ispoweroftwo()
method
i <= 0
throwing exception , want place 9 @ 0th index.
secondly haven't initialized array.
int[] newarr = new int[10];
otherwise throw nullpointerexception
want 1st,2nd, 4th , 8th indices...
try following condition in ispoweroftwo()
method.
if (i == 1) { return true; } else if (i%2==0 && i%3!=0) { return true; }
for array
int[] arr = {1,0,1,1,0,1,0,1,0,1};
it give you.
1,9,9,1,9,1,0,1,9,1 ^ ^ ^ ^ 1 2 4 8
Comments
Post a Comment