0
我目前遇到問題從陣列中拔出一定的價值,因爲它代表我的數組代碼如下:我如何獲得價值從一個數組的java
private List<FootballPlayer> getData() {
List<FootballPlayer> theData = new ArrayList<FootballPlayer>();
// create a Scanner and grab the data . . .
Scanner scanner = null;
String dataPath = dbLocation + File.separator + "text" + File.separator + "players.db";
String imagePath = dbLocation + File.separator + "images";
try {
scanner = new Scanner(new File(dataPath));
} catch (FileNotFoundException fnf) {
System.out.println(fnf.getMessage());
System.exit(0);
}
// scan player.db file line-by-line
scanner.useDelimiter("\n");
while (scanner.hasNext()) {
String line = scanner.next().trim();
// trim used to trim for new line
String[] bits = line.split(":");
String t = bits[0]; // title
String imgFileName = bits[1]; // image file name
int pa = Integer.parseInt(bits[2]); // pace
int sh = Integer.parseInt(bits[3]); // shooting
int dr = Integer.parseInt(bits[4]); // dribbling
int ph = Integer.parseInt(bits[5]); // physical
// create the image
ImageIcon img = new ImageIcon(imagePath + File.separator + imgFileName);
// Create the business object
FootballPlayer player = new FootballPlayer(t, img, pa, sh, dr, ph);
// add it to the list ... simple as ...
theData.add(player);
}
scanner.close();
return theData;
}
從這樣players.db文件:
Pique:DFPique.png:66:60:54:76
Ramos:DFRamos.png:79:66:60:82
Maldini:LEGMaldini.png:86:67:56:80
Pele:LEGPele.png:95:94:89:73
DiMaria:MFDiMaria.png:90:87:79:71
Neymar:MFNeymar.png:90:92:80:58
Berahino:STBerahino.png:81:70:69:55
Ronaldo:STRonaldo.png:98:99:99:89
我不確定如何從另一個類中的數組中拉出圖像路徑(.png),例如,如果選擇了Ramos。任何幫助將不勝感激
我不完全相信你的問題是什麼。你想知道如何獲得具有給定球員名字的特定FootballPlayer對象嗎?或者是有關訪問FootballPlayer對象內部圖像的問題? – cangoektas 2015-02-11 23:52:48
如果您想在另一個類中使用'theData'ArrayList,請將'getData()'方法公開。然後從任何其他類中調用該方法,並通過'theData'引用循環以獲取值。 – Prudhvi 2015-02-11 23:53:45
@prudhvi然後它會在每次需要數據時打開和解析文件,當時(大概)只需要進行一次 – 2015-02-12 00:19:59