![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Newbie
Join Date: Nov 2005
Posts: 14
Rep Power: 0
![]() |
Multi D Arrays
I was just wondering how one would sort and search a multi-D array?
I can do it with single, but I can't with multi. This doesn't work: public void sort() {
int temp;
for (int i = 0; i < intRow-1; i++) {
for (int j = i+1; j < intCol; j++) {
if (intArray[0][j] < intArray[i][0]) {
temp = intArray[i][0];
intArray[i][0] = intArray[0][j];
intArray[0][j] = temp;
}
}
}
}
public int search(int key) {
SortSearchAssign sSa= new SortSearchAssign();
boolean found = false;
int n = 0;
while (!found && n < sSa.intArray.length) {
if (intArray[n][0] == key) {
found = true;
return n;
}
n+= 1;
}
return -1;
}So if anyone could offer assistance, that would be much appreciated. |
|
|
|
|
|
#2 |
|
Newbie
Join Date: May 2005
Posts: 10
Rep Power: 0
![]() |
im wondering why do you have j=i+1 if you want to loop through the whole array use two for loops but with j starting as 0. then you just do if array[i][j] == num do whatever. im guessin the search meathod is to look at the first element on each row of the matrix looking for a certain number.
|
|
|
|
|
|
#3 |
|
Programmer
Join Date: Nov 2005
Posts: 35
Rep Power: 0
![]() |
Why are you specifying a 0 index in the nested for loop?
if (intArray[0][j] < intArray[i][0]) {
temp = intArray[i][0];
intArray[i][0] = intArray[0][j];
intArray[0][j] = temp;
}Also, where are the variables intRow, intCol, intArray coming from? I don't see them declared anywhere. In the search method: To increment n, you can just say n++...that's the general convention when incrementing in an array Also, why are you returning n and -1? You know the key you are looking for, so it might make more sense to make it a boolean function and have it return true/false instead. |
|
|
|
|
|
#4 |
|
Newbie
Join Date: Nov 2005
Posts: 14
Rep Power: 0
![]() |
I don't know, I got those out of an example and tried to use them in my program, I guess it didn't work :\
intRow, intCol, and intArray are declared in the class. |
|
|
|
|
|
#5 |
|
Programmer
Join Date: Nov 2005
Posts: 35
Rep Power: 0
![]() |
If you got it out of a book, then it's prob designed for a 1D array, hence why it dosen't work. It looks like that sorting algorithm is BubbleSort, which isn't the best sorting algorithm to begin with. Try googling "java sorting" and other such terms, and you'll find plenty of helpful links. There are other sorting algorithms out there that are better, but make assumptions on your input (like your input is an array in the case of Merge Sort).
On those variables...are they declared globally in the class, or are they declared in a method? If they are declared globally, great. If they are declared in a method, you won't be able to access them from another method because of Java's enforcement of variable scope. |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|