Java HashMap字符串,字符串数组-如何在一个键中获得某个索引? 公共类主{ 最终字符串filename=“stats.txt”; List lines=Files.readAllLines(path.get(filename));//获取文件,将其放入字符串列表数组中。 List playerInfo=new ArrayList(lines.size());//创建新的playerStats ArrayList,字符串 Scanner fs=new Scanner(新文件(文件名));//在Scanner中打开文件。 整数线性装载; 四分之一整数=4; 随机=新随机(); 双随机数; HashMap playerData=新HashMap(); public Main()引发IOException{ 用于(字符串行:行){ String[]lineValues=line.split(“,”); playerInfo.add(Arrays.asList(lineValues)); lineAmount++; } 字符串playerVitals[][]=新字符串[lineAmount][7]; 字符串播放器属性[][]=新字符串[lineAmount][19]; 对于(int i=0;i
我有这段代码,它能够加载到一个文本(csv)文件中,并将其拆分为一个名为playerInfo的数组。在将其加载到playerInfo中之后,我创建了两个名为playerVitals和playerInfo的2d数组。在把信息放在我想要的地方之后,我创建了一个hashmap,它应该保存所有这些数据。如您所见,hashmap键是一个字符串,hashmap值是一个1d字符串数组。正如你所看到的,在那之后,我试着打印出数据,以在某一点上获得。在本例中,它将位于例如键“James”和索引5处。在我的电子表格上,这与年龄相符。那么,对于键“James”和索引5,我如何得到这个玩家在这一点上的年龄是我的电子表格中的数据呢?我对你的代码做了一些清理。有很多更改,所以最好检查一下新代码,看看您是否能够理解更新的内容。代码中混合了注释来解释更改 对于这个问题,在您的原始代码中,一旦检索到玩家的数据数组,年龄值位于偏移量6:Java HashMap字符串,字符串数组-如何在一个键中获得某个索引? 公共类主{ 最终字符串filename=“stats.txt”; List lines=Files.readAllLines(path.get(filename));//获取文件,将其放入字符串列表数组中。 List playerInfo=new ArrayList(lines.size());//创建新的playerStats ArrayList,字符串 Scanner fs=new Scanner(新文件(文件名));//在Scanner中打开文件。 整数线性装载; 四分之一整数=4; 随机=新随机(); 双随机数; HashMap playerData=新HashMap(); public Main()引发IOException{ 用于(字符串行:行){ String[]lineValues=line.split(“,”); playerInfo.add(Arrays.asList(lineValues)); lineAmount++; } 字符串playerVitals[][]=新字符串[lineAmount][7]; 字符串播放器属性[][]=新字符串[lineAmount][19]; 对于(int i=0;i,java,Java,我有这段代码,它能够加载到一个文本(csv)文件中,并将其拆分为一个名为playerInfo的数组。在将其加载到playerInfo中之后,我创建了两个名为playerVitals和playerInfo的2d数组。在把信息放在我想要的地方之后,我创建了一个hashmap,它应该保存所有这些数据。如您所见,hashmap键是一个字符串,hashmap值是一个1d字符串数组。正如你所看到的,在那之后,我试着打印出数据,以在某一点上获得。在本例中,它将位于例如键“James”和索引5处。在我的电子表格
public class Main{
final String filename = "stats.txt";
List<String> lines = Files.readAllLines(Paths.get(filename)); //gets file, puts it in to listarray of strings.
List<List<String>> playerInfo = new ArrayList<>(lines.size()); //creates new playerStats arraylist, Strings
Scanner fs = new Scanner(new File(filename)); //opens file in scanner.
int lineAmount;
int quarters = 4;
Random random = new Random();
double randomNum;
HashMap<String, String[]> playerData = new HashMap<String, String[]>();
public Main() throws IOException {
for (String line : lines) {
String[] lineValues = line.split(",");
playerInfo.add(Arrays.asList(lineValues));
lineAmount++;
}
String playerVitals[][] = new String[lineAmount][7];
String playerAttributes[][] = new String[lineAmount][19];
for (int i = 0; i < lineAmount; i++) {
playerVitals[i][0] = playerInfo.get(i).get(0); // Last Name
playerVitals[i][1] = playerInfo.get(i).get(1); // First Name
playerVitals[i][2] = playerInfo.get(i).get(2); // Position
playerVitals[i][3] = playerInfo.get(i).get(3); // Secondary Position
playerVitals[i][4] = playerInfo.get(i).get(4); // Height
playerVitals[i][5] = playerInfo.get(i).get(5); // Weight
playerVitals[i][6] = playerInfo.get(i).get(6); // Age
playerAttributes[i][0] = playerInfo.get(i).get(7); //0 to 3 feet FGA
playerAttributes[i][1] = playerInfo.get(i).get(8); // 3 to 10 feet FGA
playerAttributes[i][2] = playerInfo.get(i).get(9); //10 to 16 feet FGA
playerAttributes[i][3] = playerInfo.get(i).get(10); //16 feet to 3pt FGA
playerAttributes[i][4] = playerInfo.get(i).get(11); //3pt FGA
playerAttributes[i][5] = playerInfo.get(i).get(12); //0 to 3 feet FG
playerAttributes[i][6] = playerInfo.get(i).get(13); //3 to 10 feet FG
playerAttributes[i][7] = playerInfo.get(i).get(14); //10 to 16 feet FG
playerAttributes[i][8] = playerInfo.get(i).get(15); //16 to 3pt FG
playerAttributes[i][9] = playerInfo.get(i).get(16); //3pt FG
playerAttributes[i][10] = playerInfo.get(i).get(17); //TOV%
//playerAttributes[i][11] = playerInfo.get(i).get(18); //Athleticism
//playerAttributes[i][12] = playerInfo.get(i).get(19); //Clutch
playerAttributes[i][13] = playerInfo.get(i).get(20); //OReb%
playerAttributes[i][14] = playerInfo.get(i).get(21); //Steal%
playerAttributes[i][15] = playerInfo.get(i).get(22); //Block%
playerAttributes[i][16] = playerInfo.get(i).get(23); //DReb%
playerAttributes[i][17] = playerInfo.get(i).get(24); //Usage Rate
playerAttributes[i][18] = playerInfo.get(i).get(25); //Overall Rating;
playerData.put(playerVitals[i][0], new String[] {playerVitals[i][1], playerVitals[i][2], playerVitals[i][3], playerVitals[i][4], playerVitals[i][5], playerVitals[i][6],
playerAttributes[i][0], playerAttributes[i][1], playerAttributes[i][2], playerAttributes[i][3], playerAttributes[i][4], playerAttributes[i][5], playerAttributes[i][6], playerAttributes[i][7],
playerAttributes[i][8], playerAttributes[i][9], playerAttributes[i][10], playerAttributes[i][11], playerAttributes[i][12], playerAttributes[i][13], playerAttributes[i][14], playerAttributes[i][15],
playerAttributes[i][16], playerAttributes[i][17], playerAttributes[i][18]});
}
System.out.println(Arrays.toString(playerData.get(playerVitals[0][0])));
}
public static void main(String[] args) throws FileNotFoundException,IOException{
new Main();
}
}
这是使用说明存储年龄的注释:
String lastName = playerVitals[0][0]; // For example
String[] onePlayerData = playerData.get(lastName);
String age = onePlayerData[6];
在下面修改的代码中,值将改为:
playerVitals[i][6] = playerInfo.get(i).get(6); // Age
以下是修改后的代码:
String lastName = "James"; // For example
String[] onePlayerVitals = allPlayerVitals.get(lastName);
String age = onePlayerVitals[6];
包装样品;
导入java.io.FileNotFoundException;
导入java.io.IOException;
导入java.nio.file.Files;
导入java.nio.file.path;
导入java.util.ArrayList;
导入java.util.array;
导入java.util.HashMap;
导入java.util.List;
导入java.util.Map;
//从非描述性的“Main”重命名:
公共类PlayerDataStore{
公共静态最终字符串测试文件\u NAME=“stats.txt”;
公共静态void main(字符串[]args)抛出FileNotFoundException、IOException{
PlayerDataStore playerData=新的PlayerDataStore();
加载(测试文件名);
playerData.display();
}
//生命体征和属性的名称。
//从注释转移到可用数据。
//这对于显示值很有用。
私有静态字符串[]重要名称=新St
package sample;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
// Renamed from the undescriptive 'Main':
public class PlayerDataStore {
public static final String TEST_FILE_NAME = "stats.txt";
public static void main(String[] args) throws FileNotFoundException,IOException {
PlayerDataStore playerData = new PlayerDataStore();
playerData.load(TEST_FILE_NAME);
playerData.display();
}
// Names of the vitals and attributes.
// Shifted from comments to usable data.
// This is useful for displaying the values.
private static String[] VITAL_NAMES = new String[] {
"Last Name",
"First Name",
"Position",
"Secondary Position",
"Height",
"Weight",
"Age"
};
public static final int NUM_VITALS = VITAL_NAMES.length;
private static String[] ATTR_NAMES = new String[] {
"0 to 3 feet FGA",
"3 to 10 feet FGA",
"10 to 16 feet FGA",
"16 feet to 3pt FGA",
"3pt FGA",
"0 to 3 feet FG",
"3 to 10 feet FG",
"10 to 16 feet FG",
"16 to 3pt FG",
"3pt FG",
"TOV%",
"Athleticism",
"Clutch",
"OReb%",
"Steal%",
"Block%",
"DReb%",
"Usage Rate",
"Overall Rating"
};
public static final int NUM_ATTRS = ATTR_NAMES.length;
//
// Split the storage into separate 'vitals' and 'attributes' storage.
// Putting both sets of data -- vitals, and attributes -- was a bit messy,
// and seemed unnecessary.
private Map<String, String[]> allPlayerVitals;
private Map<String, String[]> allPlayerAttributes;
// Moved static code, and other code which was under 'Main', to a descriptive method.
// Unused values were removed.
public void load(String filename) throws FileNotFoundException, IOException {
// First load the line data, then process the data into the stores.
// Note: Storing the line values into 'playerInfo' is not necessary:
// The line values can be used directly to populate the vitals and
// attributes.
List<String> lines = Files.readAllLines( Paths.get(filename) );
int lineAmount = lines.size();
List<List<String>> playerInfo = new ArrayList<>(lineAmount);
for ( String line : lines ) {
String[] lineValues = line.split(",");
playerInfo.add( Arrays.asList(lineValues) );
}
// Make space for the vitals and attributes ...
allPlayerVitals = new HashMap<String, String[]>( lineAmount );
allPlayerAttributes = new HashMap<String, String[]>( lineAmount );
// Transfer data from the vitals and attributes into storage ...
for ( List<String> nextInfo : playerInfo ) {
String playerVitals[] = new String[NUM_VITALS];
for ( int vitalNo = 0; vitalNo < NUM_VITALS; vitalNo++ ) {
playerVitals[vitalNo] = nextInfo.get(vitalNo);
}
// 'playerVitals[0]' is the players last name, which is currently
// a unique value.
//
// TODO: The uniqueness of the player last name seems overly
// optimistic. A different key value may be needed
allPlayerVitals.put( playerVitals[0], playerVitals );
String playerAttributes[] = new String[19];
for ( int attrNo = 0; attrNo < 19; attrNo++ ) {
if ( (attrNo == 11) || (attrNo == 12) ) {
continue;
}
playerAttributes[attrNo] = nextInfo.get(NUM_VITALS + attrNo);
}
// Store the attributes using the player last name.
allPlayerAttributes.put( playerVitals[0], playerAttributes );
}
}
void display() {
// Display all stored data.
// Note: The order of entries is random, since it is an iteration across a
// hash mapping.
for ( Map.Entry<String, String[]> vitalsEntry : allPlayerVitals.entrySet() ) {
String playerLastName = vitalsEntry.getKey();
String[] playerVitals = vitalsEntry.getValue();
String[] playerAttributes = allPlayerAttributes.get(playerLastName);
for ( int vitalNo = 0; vitalNo < playerVitals.length; vitalNo++ ) {
System.out.println("[ " + vitalNo + " ] " + VITAL_NAMES[vitalNo] + ": " + playerVitals[vitalNo] );
}
for ( int attrNo = 0; attrNo < playerAttributes.length; attrNo++ ) {
if ( (attrNo == 11) || (attrNo == 12) ) {
continue;
}
System.out.println("[ " + attrNo + " ] " + ATTR_NAMES[attrNo] + ": " + playerAttributes[attrNo] );
}
}
}
}