Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Help with array List

 
Elvis Ve
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 348
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 34178
340
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
chi Lin
Ranch Hand
Posts: 348
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 815
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
just a word of warning... cross posting tends to be frowned upon...
 
Marilyn de Queiroz
Sheriff
Posts: 9059
12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dirk moved the other thread about this subject to the Java in General (intermediate) forum. Please respond there and do not respond here. Thanks.
 
Elvis Ve
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 7023
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Elvis,

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

I'm moving this to the Intermediate forum.

Please continue there.
 
Elvis Ve
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
anybody?..... this sort thing is really making me mad
 
fei long
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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()
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic