기본적으로 .csv 파일에 여러 사람의 데이터가 있습니다. 이 스프레드 시트 파일에는 한 열에있는 사람들의 이름과 그 뒤에 다른 두 열에서의 나이 및 작업 경험이 포함됩니다.선택 정렬 - 배열 목록
지금까지 텍스트 영역에 데이터를 올바르게 표시했습니다. 그러나 사람들이 두 개의 버튼 중 하나를 클릭 할 때 알파벳순으로 이름순으로 정렬하거나 나이를 오름차순으로 정렬 할 수 있기를 바랍니다. 이 예제에서는 나이별로 정렬하려고 시도하여 복잡성을 제한하려고 시도했습니다.
나는 이것을하기 위해서 말했고, 나는 선택 정렬 알고리즘을 사용해야한다. 불행히도, 나는 arrayLists가 아닌 배열의 선택 정렬 만 사용했으며 .csv 파일에 저장된 데이터는 사용하지 않았습니다.
내가 겪은 문제점은 데이터 (arrayLists)를 거쳐 최소 위치에 다시 할당하는 방법이 없다는 것입니다. 최소 위치 방법을 참조하십시오.
public class Inputs extends JFrame
{
ArrayList <People> pList = new ArrayList <People>();
JButton NameSortButton;
JTextArea DisplayTextArea;
String outputText = "";
public Inputs()
{
// Construct the GUI
class innerListener implements ActionListener
{
public void actionPerformed (ActionEvent myActionEvent)
{
if (myActionEvent.getSource() == AgeSortButton)
{
sortByAge();
}
}
}
ActionListener inListener = new innerListener();
NameSortButton.addActionListener(inListener);
}
public void readPeopleData()
{
FileReader reader = null;
int lineNumber = 1;
try
{
reader = new FileReader("People.csv");
Scanner in = new Scanner(reader);
while (in.hasNextLine())
{
String input = in.nextLine();
String section[] = input.split(",");
pList.add(new People(section[0], section[1], Integer.parseInt(section[2]), Integer.parseInt(section[3])));
lineNumber++;
}
for (People p: pList)
{
String heading = "Name \tAge \tWork Experience";
outputText = outputText + p.getPersonName() +
"\t" + p.getAge() +
"\t" + p.getExperience();
DisplayTextArea.setText(heading + outputText);
}
}
catch(IOException error)
{
JOptionPane.showMessageDialog(null, "File not Found","Error" , JOptionPane.ERROR_MESSAGE);
}
} // public void readPatientData()
private void sortByAge()
{
for (int i = 0; i < pList.size(); i++)
{
int minPos = minimumPosition(i);
swap(minPos, i);
}
displayOutputs();
}
private int minimumPosition(int from)
{
int minPos = from;
for (int i = from + 1; i < pList.size(); i++)
{
// ****** PROBLEM AREA ****** //
// if (pList.get(i).getAge() < // NO IDEA)
{
minPos = i;
}
/*
The text book sorted arrays by:
for (int i = from + 1; i < array.length; i ++)
if (array[i] < array[minPos])
{
minPos = i;
}
*/
}
return minPos;
}
private void swap(int i, int j)
{
People temp = pList.get(i);
pList.set(i, pList.get(j));
pList.set(j, temp);
}
private void displayOutputs()
{
for(int j = 0; j < pList.size(); j++)
{
outputText = outputText + pList.get(j).getPersonName() +
"\t" + pList.get(j).getAge() +
"\t" + pList.get(j).getExperience() +
DisplayTextArea.setText(outputText);
System.out.println(outputText);
}
}
public static void main (String args[])
{
} // public static void main (String args[])
}
누군가가 올바른 방향으로 날 지점시겠습니까? 어떻게 정렬 된 데이터를 정렬되지 않은 데이터와 비교할 수 있습니까? 또한, 내 은 루프에 맞습니까?
미리 감사드립니다.
GAH를! 왜 사람들은 분명 기본 프로그래밍을 먼저 배워야하는 사람들에게 GUI로 숙제를해야한다고 주장합니까? ... 아, 그리고 숙제 질문에 태그를 달아 주시면 감사하겠습니다. – bdares
관련 질문 : http://stackoverflow.com/questions/1894060/sort-one-column-of-data-in-a-csv-file-in-ascending-order-in-java – Santosh