Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Jan 7th, 2006, 11:07 PM   #1
Java|Tera
Newbie
 
Join Date: Nov 2005
Posts: 14
Rep Power: 0 Java|Tera is on a distinguished road
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.
Java|Tera is offline   Reply With Quote
Old Jan 8th, 2006, 12:59 AM   #2
Blackwolf189
Newbie
 
Join Date: May 2005
Posts: 10
Rep Power: 0 Blackwolf189 is on a distinguished road
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.
Blackwolf189 is offline   Reply With Quote
Old Jan 8th, 2006, 1:05 AM   #3
elford
Programmer
 
elford's Avatar
 
Join Date: Nov 2005
Posts: 35
Rep Power: 0 elford is on a distinguished road
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;
		}
The array won't completly sort if you don't go through each element, and you can't do that with a specific index that never changes. I think it might make more sense to substiute i for 0 so that way the entire array will get looped through.

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.
elford is offline   Reply With Quote
Old Jan 8th, 2006, 6:32 PM   #4
Java|Tera
Newbie
 
Join Date: Nov 2005
Posts: 14
Rep Power: 0 Java|Tera is on a distinguished road
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.
Java|Tera is offline   Reply With Quote
Old Jan 8th, 2006, 8:56 PM   #5
elford
Programmer
 
elford's Avatar
 
Join Date: Nov 2005
Posts: 35
Rep Power: 0 elford is on a distinguished road
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.
elford is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 12:43 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC