wood burning stoves 2.0*
The moose likes Java in General and the fly likes Help with array List Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Help with array List" Watch "Help with array List" New topic
Author

Help with array List

Elvis Ve
Greenhorn

Joined: May 17, 2004
Posts: 19
hey whats up guys, Im having a problem I created a sort method in order to organize the lastNames by alphabetical order here is my sort method: why is it not working when i call the sorth method

void Sort()
{
int length = list.size();
for(int i = 0; i < length -1;i++)
{
int index = findPosition(i);
swap(index,i);
}

}

void swap(int startScan, int i)
{

Student s= (Student)list.get(i);
Student p= (Student)list.get(startScan);
Student temp = s;
s = p;
p = temp;



}


int findPosition(int i)
{
int startScan = i;
Student s = (Student)list.get(i);
for(int k =0; k < list.size()-1;k++)
{
Student d = (Student)list.get(k);
if(d.getLname().compareTo(s.getLname())< 0)
k = startScan;



}
return startScan;
}
chi Lin
Ranch Hand

Joined: Aug 24, 2001
Posts: 348
What is the problem ?

Is the sorted result not 100% correct or nothing is working ?

for(int k =0; k < list.size()-1;k++), looks like the -1 here could cause a boundary problem.
Elvis Ve
Greenhorn

Joined: May 17, 2004
Posts: 19
its not sorting at all, i dont know if i have something wrong inside but everything seems to be right
Jeanne Boyarsky
author & internet detective
Marshal

Joined: May 26, 2003
Posts: 30758
    
156

Elvis,
The problem lies in the swap method. At the end of the method, you have swapped the variables that "s" and "p" are pointing to. However, you did not set the values back to the array. So the array has the same values and the swap is not occuring.


[Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Blogging on Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, OCAJP, OCPJP beta, TOGAF part 1 and part 2
chi Lin
Ranch Hand

Joined: Aug 24, 2001
Posts: 348
inside findPosition(int i), looks to me the i passed into the
findPosition() was returned again.

(i assign to startScan, startScan then returned without any change)
Nick George
Ranch Hand

Joined: Apr 04, 2004
Posts: 815
just a word of warning... cross posting tends to be frowned upon...


I've heard it takes forever to grow a woman from the ground
Marilyn de Queiroz
Sheriff

Joined: Jul 22, 2000
Posts: 9047
    
  10
Dirk moved the other thread about this subject to the Java in General (intermediate) forum. Please respond there and do not respond here. Thanks.


JavaBeginnersFaq
"Yesterday is history, tomorrow is a mystery, and today is a gift; that's why they call it the present." Eleanor Roosevelt
Elvis Ve
Greenhorn

Joined: May 17, 2004
Posts: 19
I made some fixes to it but for some reason it still wont sort, check it out: what is wrong with it?, this is driving me crazy, please help and thanks in advance


void Sort()
{
int length = list.size();
for(int i = 0; i < length -1;i++)
{
int index = findPosition(i);
swap(index,i);
}

}

void swap(int startScan, int i)
{

Student s = (Student)list.get(i);
list.set(i, list.get(startScan));
list.set(startScan, s);

}


int findPosition(int i)
{
int startScan = i;
Student s = (Student)list.get(i);
for(int k =0; k < list.size()-1;k++)
{
Student d = (Student)list.get(k);
if(d.getLname().compareTo(s.getLname())< 0)
startScan = k;



}
return startScan;
}
Elvis Ve
Greenhorn

Joined: May 17, 2004
Posts: 19
by the way here is my test class just in case i made a mistake there and the sorting is not working because of this:

import javax.swing.JOptionPane;
import javax.swing.JTextArea;
import javax.swing.JScrollBar;
import javax.swing.JScrollPane;


class Driver
{
public static void main(String arg[])
{

Filing f = new Filing();



int m;


do
{
m = getData.getInt("Choose from the follwing menu:\n1- add a Student without GPA. \n2- add a Student with GPA. \n3- change a Student's current GPA\n4-Print\n5-Exit");


switch(m)
{
case 1:
String id = getData.getString("enter student's id");
String lName = getData.getString("enter student's last name");
String fName = getData.getString("enter student's first name");
Student s = new Student(id,lName,fName);
Undergraduate u = new Undergraduate(s);
f.addToList(s);

break;
case 2:
String Id = getData.getString("enter student's id");
String LName = getData.getString("enter student's last name");
String FName = getData.getString("enter student's first name");
double gPA = getData.getDouble("enter the student's GPA");
Student t = new Student(Id,LName,FName,gPA);
Undergraduate n = new Undergraduate(t);
f.addToList(t);
break;
case 3:
String tempID = getData.getString("enter student's id");
double NewGPA = getData.getDouble("enter the student's GPA");
if(f.search(tempID) == true)




break;
case 4:
f.Sort();
JTextArea text = new JTextArea(f.toString(),15,40);
JScrollPane scroll = new JScrollPane(text);
JOptionPane.showMessageDialog(null, scroll,"Student Information", JOptionPane.INFORMATION_MESSAGE);
break;
case 5:
break;
default:
break;


}

}
while(m!=5);



}
}
Dirk Schreckmann
Sheriff

Joined: Dec 10, 2001
Posts: 7023
Elvis,

This isn't the best forum around here for this conversation.

I'm moving this to the Intermediate forum.

Please continue there.


[How To Ask Good Questions] [JavaRanch FAQ Wiki] [JavaRanch Radio]
Elvis Ve
Greenhorn

Joined: May 17, 2004
Posts: 19
anybody?..... this sort thing is really making me mad
fei long
Ranch Hand

Joined: Apr 04, 2002
Posts: 48
I am not quite sure how would you like to sort. But I noticed that you might have to change
-------------------------------------
int startScan = i;
for(int k =0; k < list.size()-1;k++)
{
Student d = (Student)list.get(k);
if(d.getLname().compareTo(s.getLname())< 0)
startScan = k;
----------------------------------------
k=0 --> k=i
in findPosition()
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Help with array List