java - Can't add integer to an array without a 0 at first index -
i used arraylist
@ first. course of study teacher told me can't utilize arraylist
in program. said can utilize arrays.
the problem when add together integer array, puts 0 on first index.
here code :
int[] handbag = new int[1]; boolean isit = true; { int[] newbag = new int[bag.length + 1]; string name = scanner.next(); if (name.equals("a")){ int number = scanner.nextint(); (int = 0; < bag.length; i++) { newbag[newbag.length - 1] = number; newbag[i] = bag[i]; } handbag = newbag; system.out.println(number + " added bag."); } } while (isit == true);
please help me guys. can't create minimum , size operations without right add together operation.
you start single-element array, , add together sec element it:
int[] newbag = new int[bag.length + 1];
thus time you've read 1 number array contains 2 elements (i.e. 1 element many).
to right this, need alter the
int[] handbag = new int[1];
to
int[] handbag = new int[0];
if looks odd, see why java allow arrays of size 0?
you'll need move
newbag[newbag.length - 1] = number;
out of loop.
java arrays add
No comments:
Post a Comment