show elements in correct order
So I have two arrays with values iArray {A,C,E,B,H,C,B,F,D} and tArray{M,D,F,E,M,H,C,A,K}
My code shows results like this (if I choose letter B):
E,F
C,D
H,M
But it shoud be like this:
E,F,A,M
C,D,K
H,M
Can you tell me how to fix it?
Code:
for(int z = 0; z < counter; z++)
{
c:
for(int i = 0; i < counter; i++)
{
fta = iArray[i];
sta = tArray[i];
if(fta.equals(oldsta))
{
textArea2.append(sta+" ");
oldsta = sta;
oldfta = fta;
kk++;
finalArray[i] = tArray[i];
index = i;
iArray[i] = "0";
tArray[i] = "0";
break c;
}
else if(sequence.equals(fta))
{
if(index != 0)
{
textArea1.append(finalArray[index]+"\n");
textArea2.append("\n"+sta+" ");
oldsta = sta;
oldfta = fta;
kk++;
finalArray[i] = tArray[i];
index = i;
iArray[i] = "0";
tArray[i] = "0";
break c;
}
else{
textArea2.append(sta+" ");
oldsta = sta;
oldfta = fta;
kk++;
finalArray[i] = tArray[i];
index = i;
iArray[i] = "0";
tArray[i] = "0";
break c;
}
}
else{
loop:
for(int ii = 0; ii < kk; ii++)
{
if(fta.equals(oldfta))
{
textArea1.append(finalArray[index]+"\n");
textArea2.append("\n"+tArray[i]+" ");
oldsta = tArray[i];
finalArray[i] = tArray[i];
index = i;
iArray[i] = "0";
tArray[i] = "0";
break loop;
}
}
}
}
}
textArea1.append(finalArray[index]+"\n");
Re: show elements in correct order
Quote:
Originally Posted by
Shien
So I have two arrays with values iArray {A,C,E,B,H,C,B,F,D} and tArray{M,D,F,E,M,H,C,A,K}
My code shows results like this (if I choose letter B):
E,F
C,D
H,M
But it shoud be like this:
E,F,A,M
C,D,K
H,M
Why should it be like that? Can you tell us the reason why?
kind regards,
Jos
Re: show elements in correct order
I am writing program wich would show a path from letter you entered to the point it ends. So that is why there is 2 arrays: if array and then array;
It's kinda similar to a tree. you go from a top down. In example above I showed how it should be.
Sorry I am a bit rusty on my english so I can't explain this very well :/
Re: show elements in correct order
I'd iterate over the iArray, trying to find a wanted symbol and I'd recurse over both arrays to find and print the nodes on a current 'branch'.
kind regards,
Jos
Re: show elements in correct order
Your code is very hard to follow written like this. if you compartmentalised it that would make it easier to follow. anyhow, i had a look at your code and i understand what you are trying to do.
1. Given a letter in the first array, get the index of that letter in the second array
Code:
public int getPath(char c, char[] iArr, char[] tArr) {
int index = Arrays.binarySearch(iArr, c);
if (index>=0) return Arrays.binarySearch(tArr, c);
return index;
}
2. Handle char not found
Code:
int index = getPath('b', iArr, tArr);
if (index >= 0) {
//continue
} else {
//end
}
3. Set up a recursive method to explore one path
Code:
public void explorePath(char c, char[] iArr, char[] tArr) {
//find the char in iArr
...
//if found, output the char and recurse
...
System.out.print(newChar);
explorePath(newChar, tArr, iArr);
//if not found end the output
System.out.println();
}
I dont know if my logic is exactly right to your need but hopefully you get the idea of what I'm trying to explain (in my defense i'm answering from my smartphone and this site isnt very mobile friendly).