Java 使用for循环赋值后如何访问数组
在使用for循环为char数组赋值之后,如何访问for循环之外的值?因为我以后需要处理这些值,以删除重复的值。谢谢你的帮助。非常感谢Java 使用for循环赋值后如何访问数组,java,arrays,Java,Arrays,在使用for循环为char数组赋值之后,如何访问for循环之外的值?因为我以后需要处理这些值,以删除重复的值。谢谢你的帮助。非常感谢 public static void processLine(File input, File output) throws FileNotFoundException{ Scanner i = new Scanner(input); PrintStream o = new PrintStream(output); while(i.hasNextLine()){
public static void processLine(File input, File output) throws FileNotFoundException{
Scanner i = new Scanner(input);
PrintStream o = new PrintStream(output);
while(i.hasNextLine()){
String text = i.nextLine();
char[] pos = new char[text.length()];
for (int x = 0; x < text.length();x++){
pos[x] = text.charAt(x);
}
}
}
publicstaticvoidprocessline(文件输入、文件输出)抛出FileNotFoundException{
扫描仪i=新扫描仪(输入);
打印流o=新打印流(输出);
而(i.hasNextLine()){
字符串text=i.nextLine();
char[]pos=新字符[text.length()];
对于(int x=0;x
正如用户3580294之前所说,只需在执行循环之前声明一个结构来存储数据即可。然后在循环中存储数据,在循环之后,您可以享受并使用它
public static void processLine(File input, File output) throws FileNotFoundException{
Scanner i = new Scanner(input);
PrintStream o = new PrintStream(output);
ArrayList<String> saved= new ArrayList<String>();
while(i.hasNextLine()){
String text = i.nextLine();
char[] pos = new char[text.length()];
for (int x = 0; x < text.length();x++){
pos[x] = text.charAt(x);
}
saved.add(text);
}
// you can use "saved" here ! :) but this code can be shorter I think
}
最终结果应为:
public static void processLine(File input, File output) throws FileNotFoundException{
Scanner i = new Scanner(input);
PrintStream o = new PrintStream(output);
ArrayList<String> saved= new ArrayList<String>();
while(i.hasNextLine()){
String text = i.nextLine();
saved.add(text);
}
// you can use "saved" here ! :) enjoy
}
publicstaticvoidprocessline(文件输入、文件输出)抛出FileNotFoundException{
扫描仪i=新扫描仪(输入);
打印流o=新打印流(输出);
ArrayList saved=新建ArrayList();
而(i.hasNextLine()){
字符串text=i.nextLine();
保存。添加(文本);
}
//你可以在这里使用“saved!”:)尽情享受吧
}
在外部声明数组loop@user3580294它在for循环之外,你不是说在while循环之外吗?@blgt我更想的是while
循环……如果它在while循环之外,如何确定字符串的长度以知道所需数组的大小?
saved.add(text);
public static void processLine(File input, File output) throws FileNotFoundException{
Scanner i = new Scanner(input);
PrintStream o = new PrintStream(output);
ArrayList<String> saved= new ArrayList<String>();
while(i.hasNextLine()){
String text = i.nextLine();
saved.add(text);
}
// you can use "saved" here ! :) enjoy
}