int [] f = {1,2,3,4,5,5,4,3,2,1};
int [] b = {6,1};
System.out.println(Arrays.toString(hide(f,b)));
public static int [] hide(int [] front, int [] back) {
int temp;
int extraTemp;
int nextTemp = 0;
int [] hiddenAt = new int[front.length];
//int [] shownAt = new int[front.length];
for(int x = 0; x < front.length; x++){
for(int y = 0; y <= back.length; y++){
temp = x;
if ((back.length > front.length) || (front[x] < 0 || back[y] < 0) || (front.length < 1 || back.length < 1)) {
System.exit(0);
}
if (y < back.length - 1){
nextTemp = Math.abs(back[y + 1] - front[x + 1]);
}
else {
nextTemp = 0;
}
if (front[x] > back[y]) {
System.out.println(temp);
}
else if (front[x] < back[y] && y >= back.length - 1 ) {
extraTemp = back[y] - front[x];
if (extraTemp > nextTemp){
extraTemp = nextTemp;
}
System.out.println(extraTemp);
}
else if (front[x] < back[y]) {
extraTemp = back[y] - front[x];
if (extraTemp > nextTemp){
extraTemp = nextTemp;
}
System.out.println(extraTemp);
}
}
}
return hiddenAt;
}
The println
's are being substituted in to see the values that are being produced. They will be hiddenAt[z] = temp;
when I figure out the values to be correct.
I need it to compare front[0]
with back[0]
then front[1]
with back[1]
, etc. After that, it will shift over one: front[1]
with back[1]
then front[2]
with back[2]
until the end of front[]
. I want to find out where the lowest difference is. (In this case when the number in the []'s is 4 and 5.)
I get two errors, one saying line 39 if ((back.length > front.length) || (front[x] < 0 || back[y] < 0) || (front.length < 1 || back.length < 1)) { System.exit(0); }
is wrong and that line 9 is wrong as well. public class 2 { public static void main(String[] args) {
int [] f = {1,2,3,4,5,5,4,3,2,1};
int [] b = {6,1};
System.out.println(Arrays.toString(hide(f,b)));
}
the exact error is:
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 2
at hw2.hide(hw2.java:39)
at hw2.main(hw2.java:9)
Your problem is that Array index out of bounds so you are trying to access an element that doesn't exist.
in the line if ((back.length > front.length) || (front[x] < 0 || back[y] < 0) || (front.length < 1 || back.length < 1)) { where you say back[y] < 0
your for statement goes to <= back.length
for(int y = 0; y <= back.length; y++){
so back has a length of 2(has 2 elements) however you will be doing this loop three times 0,1,2 on the third loop there is no back[2]
as back[0] = 6
and back[1] = 1
.
so changing for(int y = 0; y <= back.length; y++){
to for(int y = 0; y < back.length; y++){
will fix this particular error
Remember, lengths start at 1(if it has elements) indexes start at 0.
This is your 2nd array problem:
if (y < back.length - 1){
nextTemp = Math.abs(back[y + 1] - front[x + 1]);
}
else {
nextTemp = 0;
}
While you check that y < back.length - 1
you do not do the same for x
.
So, you need if (y < back.lenth-1 && x < front.length-1)
assuming that you do want to reference front[x+1]
.
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.