要求不能用array list来构成程序,但是我已经用用array list写完了整个程序。
//This program will search and sort wiigame table
import java.io.;
import java.text.;
import java.util.ArrayList;
import java.util.Collections;
// Main class
public class willgame_Jiaheng
{
public static void main (String[] args)
{
ArrayList list = putFileToArray ("WiiGame.txt");
printTable (list);
do
{
//ask user choice
System.out.println ("Please enter a number to select");
System.out.println ("1)Sorting by Game");
System.out.println ("2)Sorting by Genre");
System.out.println ("3)Sorting by Release");
System.out.println ("4)Searching by Game...");
System.out.println ("5)Searching by Release...");
System.out.println ("6)Searching by Price...");
System.out.println ("0)Quit");
//enter the options
int n1 = In.getInt ();
if (n1 == 0)
{
System.out.println ("quitting");
System.exit (0);
}
else if (n1 == 1)
{
ArrayList newlist = sortByColumn (list, 0);
printTable (newlist);
}
else if (n1 == 2)
{
ArrayList newlist = sortByColumn (list, 1);
printTable (newlist);
}
else if (n1 == 3)
{
ArrayList newlist = sortByColumn (list, 3);
printTable (newlist);
}
else
{
System.out.println ("Enter keyword:");
InputStreamReader reader = new InputStreamReader (System.in);
BufferedReader br = new BufferedReader (reader);
String input = "";
try
{
input = br.readLine ();
}
catch (Exception e)
{
}
//System.out.println ("Enter 1 for Game, 2 for Release, 3 for Price");
if (n1 == 4)
{
ArrayList newlist = searchByColumn (list, 0, input);
if (newlist.size () == 0)
{
System.out.println ("The game is not in the list.");
}
else
{
System.out.println (newlist.size () + " games were found.");
printTable (newlist);
}
}
else if (n1 == 5)
{
ArrayList newlist = searchByColumn (list, 3, input);
printTable (newlist);
}
else if (n1 == 6)
{
ArrayList newlist = searchByColumn (list, 5, input);
printTable (newlist);
}
}
}
while ((In.getInt ()) != '0');
}
public static ArrayList searchByColumn (ArrayList list, int colNo, String keyword)
{
ArrayList newList = new ArrayList ();
for (int i = 0 ; i < list.size () ; i++)
{
WiiGame wg = (WiiGame) (list.get (i));
String val = wg.getCol (colNo);
if (val.equals (keyword))
{
newList.add (wg);
}
}
return newList;
}
public static ArrayList sortByColumn (ArrayList list, int colNo)
{
ArrayList newList = new ArrayList ();
int count = list.size ();
String[] colList = new String [count];
for (int i = 0 ; i < list.size () ; i++)
{
WiiGame wg = (WiiGame) (list.get (i));
String val = wg.getCol (colNo);
colList [i] = val;
}
//Collections.sort(colList);
String temp;
for (int i = 0 ; i < count ; i++)
{
for (int j = i + 1 ; j < count ; j++)
{
// to compare one string with other strings
if (colList [i].compareTo (colList [j]) > 0)
{
// swapping
temp = colList [i];
colList [i] = colList [j];
colList [j] = temp;
}
}
}
for (int i = 0 ; i < count ; i++)
{
for (int j = 0 ; j < list.size () ; j++)
{
WiiGame wg = (WiiGame) (list.get (j));
if (colList [i] == wg.getCol (colNo))
{
newList.add (wg);
break;
}
}
}
return newList;
}
public static void printTable (ArrayList list)
{
System.out.println ("Game" + "\t" + " Genre" + "\t" + " Developer" + "\t" + " Release" + "\t" + "Sales" + "\t" + "Price");
System.out.println ("--------------------------------------------------------------------------------------------------------");
for (int i = 0 ; i < list.size () ; i++)
{
System.out.println (list.get (i).toString ());
}
}
public static ArrayList putFileToArray (String filePath)
{
ArrayList list = new ArrayList ();
try
{
FileInputStream fin = new FileInputStream (filePath);
InputStreamReader reader = new InputStreamReader (fin);
BufferedReader buffReader = new BufferedReader (reader);
String line = "";
while ((line = buffReader.readLine ()) != null)
{
//System.out.println(line);
String[] lineArr = line.split ("\\t");
if (lineArr.length > 1)
{
WiiGame wg = new WiiGame (lineArr [0], lineArr [1], lineArr [2], lineArr [3], lineArr [4], lineArr [5]);
//System.out.println(wg.toString());
list.add (wg);
}
}
buffReader.close ();
}
catch (IOException e)
{
}
finally
{
}
return list;
}
}
// The "WiiGame" class.
public class WiiGame
{
protected String game;
protected String genre;
protected String developer;
protected String release;
protected String sales;
protected String price;
public WiiGame(String game,String genre,String developer,String release,String sales,String price) {
this.game = game;
this.genre = genre;
this.developer = developer;
this.release =release;
this.sales = sales;
this.price = price;
}
public String getGame ()
{
return game;
}
public String getRelease ()
{
return release;
}
public String getPrice ()
{
return price;
}
public String getCol(int col)
{
if(col==0)
{
return game;
}
else if(col==1)
{
return genre;
}
else if(col==3)
{
return release;
}
else if(col==5)
{
return price;
}
else
{
return "";
}
}
public String toString ()
{
return game + "\t" + genre + "\t" + developer+ "\t" + release+ "\t" + sales+ "\t" + price;
}
}
运行结果成功
希望通过 顺序搜索,二进制搜索,插入排序,冒泡排序,希尔排序法,选择排序来替换掉arraylist
txt格式的数据文件对吗