Results 1 to 5 of 5
Thread: show elements in correct order
- 12-30-2011, 10:46 AM #1
Member
- Join Date
- Dec 2011
- Posts
- 16
- Rep Power
- 0
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?
Java 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");
- 12-30-2011, 12:44 PM #2
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,425
- Blog Entries
- 7
- Rep Power
- 17
- 12-30-2011, 01:17 PM #3
Member
- Join Date
- Dec 2011
- Posts
- 16
- Rep Power
- 0
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 :/
- 12-30-2011, 03:21 PM #4
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,425
- Blog Entries
- 7
- Rep Power
- 17
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,
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
-
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
2. Handle char not foundJava 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; }
3. Set up a recursive method to explore one pathJava Code:int index = getPath('b', iArr, tArr); if (index >= 0) { //continue } else { //end }
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).Java 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(); }Last edited by ozzyman; 12-30-2011 at 05:22 PM.
Similar Threads
-
outputting results from bubble sorted array in correct numerical order
By leoshiner in forum New To JavaReplies: 1Last Post: 11-13-2011, 06:23 PM -
JList doesn't show the elements
By mrbeast87 in forum AWT / SwingReplies: 2Last Post: 04-20-2011, 06:21 PM -
how to show web browser in mobile app or is it possible to show it in textArea
By Basit781 in forum CLDC and MIDPReplies: 3Last Post: 05-27-2010, 10:54 AM -
How to place the GUI components in correct order
By impact in forum New To JavaReplies: 2Last Post: 05-04-2008, 06:41 AM -
netbeans 6.0 not show commpunent or show blank page
By fahimaamir in forum NetBeansReplies: 1Last Post: 01-26-2008, 06:20 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks