java - Getting ArrayList.get() to return -
java - Getting ArrayList.get() to return -
i trying write a simple function translating integer, , vice versa, 3bit binary string 0 padding. test inttobinary method created simple loop adding integers 1 5 , translating each number binary string, combine binary strings larger string.
arraylist citylist = new arraylist(); string gene = ""; for(int = 1; i<5;i++){ citylist.add(i, i); gene += inttobinary(i); system.out.println(gene + "" + citylist.get(i)); } system.out.println(gene);
when tried running little bit of code error @ citylist.get(i)
shouldn't object.get(i)
method returns variable @ index i? tried initializing int
doing int citynum = (int)citylist.get(i)
got error saying object
found instead of int
.
the variable type used in object.add(int index, object)
int
method should homecoming int
variable.
if understand question, like
for (int = 0; < math.pow(2, 3); i++) { stringbuilder sb = new stringbuilder(integer.tobinarystring(i)); while (sb.length() < 3) { sb.insert(0, '0'); } system.out.printf("%d = %s%n", i, sb.tostring()); }
output is
0 = 000 1 = 001 2 = 010 3 = 011 4 = 100 5 = 101 6 = 110 7 = 111
to go vice-versa can utilize integer.parseint(string,int)
like
int val = integer.parseint("001", 2); // <-- 1
java
Comments
Post a Comment