Java 读取文件并将整数存储在索引位置
我对Java相当陌生;我如何读取一个文件,然后将一个整数存储在某个索引中 下面是文件:Java 读取文件并将整数存储在索引位置,java,arrays,Java,Arrays,我对Java相当陌生;我如何读取一个文件,然后将一个整数存储在某个索引中 下面是文件: 17 15 27 7 19 4 29 101 22 29 14 6 14 89 22 47 28 28 29 69 2 27 28 18 7 10 19 90 13 55 18 96 5 7 6 32 26 51 25 65 29 54 14 79 4 17 5 59 20 6 0 55 5 38 我想把它们成对地读出来,第一个数字是索引,第二个数字是要存储在索引中的数字 array[17] = 15 a
17 15 27 7 19 4 29 101 22 29 14 6 14 89 22 47 28 28 29 69 2 27 28 18 7 10 19 90 13 55 18 96 5 7 6 32 26 51 25 65 29 54 14 79 4 17 5 59 20 6 0 55 5 38
我想把它们成对地读出来,第一个数字是索引,第二个数字是要存储在索引中的数字
array[17] = 15
array[27] = 7
array[19] = 4
以此类推,直到文件没有更多的整数可读取为止
更新
另外,如果有区别,我有一个2D数组来存储它,首先将输入作为字符串,然后使用
string.split(“”)将其拆分为数组。然后使用integer.parseInt()
将数组中的每个值转换为相应的整数值,并将数组命名为A
。
创建一个数组B
,其大小等于(数组A
中的最大值位于偶数位置+1)。Ans然后再次遍历数组A
,以在相应位置设置值。实现此目的的几种方法
我一直喜欢的一种好方法是使用Properties类。添加了一个代码段以显示类的工作方式。:)如果你有问题,请告诉我
public void writeToProp(){
Properties defaultProperties = new Properties();
defaultProperties.setProperty("17", "15");
defaultProperties.setProperty("27", "7");
defaultProperties.setProperty("19", "4");
try (FileOutputStream stream = new FileOutputStream(PATH_TO_SAVE_YOUR_FILE)) {
defaultProperties.store(stream, null);
stream.close();
} catch (Exception ex) {
ex.toString();
}
}
public Properties readFromProp(){
Properties prop = new Properties();
InputStream stream = null;
try {
stream = new FileInputStream(PATH_TO_SAVE_YOUR_FILE);
prop.load(stream);
} catch (IOException ex) {
ex.printStackTrace();
} finally {
if (stream != null) {
try {
stream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
public void getValue(String key,String value, Properties properties) {
properties.setProperty(key, value);
}
public String setValue(String key, Properties properties) {
return properties.getProperty(key);
}
你可以用这个简单的片段
int array[] = new int[SIZE];
try(BufferedReader br = new BufferedReader(new FileReader(file))) {
for(String line; (line = br.readLine()) != null; ) {
String[] split = line.trim().split(" ");
for(int i = 0; i+1 < split.length; i++){
array[Integer.parseInt(split[i])] = Integer.parseInt(split[i+1]);
}
}
}
int数组[]=新的int[SIZE];
try(BufferedReader br=new BufferedReader(new FileReader(file))){
对于(字符串行;(line=br.readLine())!=null;){
String[]split=line.trim().split(“”);
对于(int i=0;i+1
对于二维阵列
int array[][] = new int[SIZE][];
try(BufferedReader br = new BufferedReader(new FileReader(file))) {
for(String line; (line = br.readLine()) != null; ) {
String[] split = line.trim().split(" ");
for(int i = 0; i+2 < split.length; i++){
int j = Integer.parseInt(split[i]);
int k = Integer.parseInt(split[i + 1]);
int l = Integer.parseInt(split[i + 2]);
array[j][k] = l;
}
}
}
int数组[][]=新的int[SIZE][];
try(BufferedReader br=new BufferedReader(new FileReader(file))){
for(字符串行;(line=br.readLine())!=null;){
String[]split=line.trim().split(“”);
对于(int i=0;i+2
您还可以使用扫描仪读取文件
Scanner sc = new Scanner(new File("nums.txt"));
然后运行一个while循环,直到文档没有更多内容为止。比如:
public class ReadNums {
public static void main(String[] args) {
Scanner sc = new Scanner(new File("nums.txt"));
int[][] A = new int[160][30];
while (sc.hasNext()) {
String s = sc.readNext();
String s2 = sc.readNext();
int num1 = Integer.parseInt(s);
int num2 = Integer.parseInt(s);
A[num1][num2];
}
}
}
因此,阅读文本,用分隔符(“
”)分隔行,您现在有了一个数组,其中奇数索引是目标数组的索引,偶数值是值。您需要预先知道目标数组的最大索引,这样就不会得到索引outofbounds
exception@MadProgrammer如果有区别的话,我的数组是2D数组,最大索引是array[160][30](我已经用-1填充了它,我在索引中读到的任何内容都将替换该索引中的-1值,然后如果索引中没有-1,我将打印它。好吧,你应该将这些细节添加到你的问题中,因为我知道我是完全正确的。)confused@TheMarkofDom如果您的数组是2D数组,则必须以160 30 25的形式提供i和j i.e输入(25将存储在arr[160][30])@markofdom是2D数组的情况,如果您只提供一个值作为位置,那么您需要将其转换为i和j,方法是将其除以不带列的值来标识行,剩余部分将是列。老实说,我不知道这是如何工作的,我还没有了解它。