Java 使用BufferedReader的递归
在我的comp-sci课程中,我们遇到了一个我似乎无法解决的问题。问题如下。我们还必须使用洪水填充算法 创建一个程序,读取包含15 x 15网格的文本文件,该网格表示人类细胞。如果这些细胞是健康细胞,则用加号“+”,如果它们是癌细胞,则用减号“-”。网格的外部行和列将只包含加号“+”。基本上,您需要使用递归来检查txt文件中的每个项目,如果字符是“-”,则将其更改为“”,如果字符是“+”,则将其保留为“+” 这是我目前拥有的代码。到目前为止,我已经将文本文件放入arraylist,然后制作了一个字符串数组。我不确定如何检查每个值,看它们是否为“-”,因为一旦我能够做到这一点,递归应该相当简单。任何帮助都将不胜感激Java 使用BufferedReader的递归,java,arrays,recursion,arraylist,bufferedreader,Java,Arrays,Recursion,Arraylist,Bufferedreader,在我的comp-sci课程中,我们遇到了一个我似乎无法解决的问题。问题如下。我们还必须使用洪水填充算法 创建一个程序,读取包含15 x 15网格的文本文件,该网格表示人类细胞。如果这些细胞是健康细胞,则用加号“+”,如果它们是癌细胞,则用减号“-”。网格的外部行和列将只包含加号“+”。基本上,您需要使用递归来检查txt文件中的每个项目,如果字符是“-”,则将其更改为“”,如果字符是“+”,则将其保留为“+” 这是我目前拥有的代码。到目前为止,我已经将文本文件放入arraylist,然后制作了一个
public class Cancer {
public static void main(String[] args) throws IOException {
String[][] grid = new String[14][14];
BufferedReader in = new BufferedReader(new FileReader("location.txt"));
String str=null;
ArrayList<String> lines = new ArrayList<String>();
while((str = in.readLine()) != null){
lines.add(str);
}
String[] linesArray = lines.toArray(new String[lines.size()]);
}
}
公共级癌症{
公共静态void main(字符串[]args)引发IOException{
字符串[][]网格=新字符串[14][14];
BufferedReader in=新的BufferedReader(新文件读取器(“location.txt”);
字符串str=null;
ArrayList行=新的ArrayList();
而((str=in.readLine())!=null){
行。添加(str);
}
String[]linesArray=lines.toArray(新字符串[lines.size()]);
}
}
首先,我认为最好使用字符网格,但让我们继续使用字符串方法:
声明数组时,声明其大小,因此包含15个元素的字符串数组将声明为新字符串[15]
我真的不明白为什么需要二维字符串数组,因为一个字符串就是整行(行)。
下面是一个稍加修改的程序版本,它将文件的行读入一维字符串数组,然后在每行中的每个字符上循环并查找“癌症”(迭代):
import java.util.*;
导入java.io.*;
公共级癌症{
公共静态void main(字符串[]args)引发IOException{
字符串[]网格=新字符串[15];
BufferedReader in=新的BufferedReader(新文件读取器(“location.txt”);
字符串str=null;
ArrayList行=新的ArrayList();
int i=0;
而((str=in.readLine())!=null){
网格[i]=str;
i++;
}
对于(int x=0;x
希望这能让您了解如何检查输入。
字符串和字符的方法没有问题(因为“网格”变成了一维),它只是使实际的“算法”的可移植性降低了一点。正如其他人所提到的,使用递归解决这个问题有点奇怪。一种方法是利用网格坐标进行递归调用。另一种方法是“切掉”“数组/字符串。以下是我的尝试:
package com.college.assignment;
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
/**
* @author Kunal Krishna
*
*/
public class Cancer {
public static final String cancerSamplePath = "C://Users//Kunal//workspace//Cancer///src//com//college//assignment//location.txt";
static List<String> lines = new ArrayList<>(0);
public static void main(String[] args) {
readFile();
displayFile();
System.out.println("\n========================================");
if (searchInfectedCells(lines) == 0) {
System.out.println("None of the Cells are damaged");
}
System.out.println("========================================");
}
private static void displayFile() {
for (String string : lines) {
System.out.println(string);
}
}
private static int searchInfectedCells(List<String> lines) {
int infectedCellRowPosition = 0;
int totalInfectedCells = 0;
for (String row : lines) {
infectedCellRowPosition++;
for (int infectedCellColPosition = 0; infectedCellColPosition < row
.length(); infectedCellColPosition++) {
if (row.charAt(infectedCellColPosition) == '-') {
totalInfectedCells++;
System.out.println("Cancerous cell found at location ("
+ infectedCellRowPosition + ","
+ infectedCellColPosition + ")");
}
}
}
return totalInfectedCells;
}
public static void readFile() {
BufferedReader in;
String str;
try {
in = new BufferedReader(new FileReader(cancerSamplePath));
while ((str = in.readLine()) != null) {
lines.add(str);
}
} catch (FileNotFoundException fne) {
System.out.println("Cancer sample data missing.");
fne.printStackTrace();
} catch (IOException ioe) {
ioe.printStackTrace();
}
}
}
奇怪的问题。这种映射操作通常是迭代完成的。此外,文本中的实际问题似乎与标题无关。这不是递归适用的问题。您确定要使用递归吗?@KEYSER
网格数组不是非必需的吗?使用迭代器
我们可以获取String@KNU因为我们只处理一次,不,这不是必需的。但这有利于良好的实践:)
package com.college.assignment;
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
/**
* @author Kunal Krishna
*
*/
public class Cancer {
public static final String cancerSamplePath = "C://Users//Kunal//workspace//Cancer///src//com//college//assignment//location.txt";
static List<String> lines = new ArrayList<>(0);
public static void main(String[] args) {
readFile();
displayFile();
System.out.println("\n========================================");
if (searchInfectedCells(lines) == 0) {
System.out.println("None of the Cells are damaged");
}
System.out.println("========================================");
}
private static void displayFile() {
for (String string : lines) {
System.out.println(string);
}
}
private static int searchInfectedCells(List<String> lines) {
int infectedCellRowPosition = 0;
int totalInfectedCells = 0;
for (String row : lines) {
infectedCellRowPosition++;
for (int infectedCellColPosition = 0; infectedCellColPosition < row
.length(); infectedCellColPosition++) {
if (row.charAt(infectedCellColPosition) == '-') {
totalInfectedCells++;
System.out.println("Cancerous cell found at location ("
+ infectedCellRowPosition + ","
+ infectedCellColPosition + ")");
}
}
}
return totalInfectedCells;
}
public static void readFile() {
BufferedReader in;
String str;
try {
in = new BufferedReader(new FileReader(cancerSamplePath));
while ((str = in.readLine()) != null) {
lines.add(str);
}
} catch (FileNotFoundException fne) {
System.out.println("Cancer sample data missing.");
fne.printStackTrace();
} catch (IOException ioe) {
ioe.printStackTrace();
}
}
}
+++++++++++++++
+-+++++++++++++
+++++++++++++++
+++++++++++++++
+++++++++++++++
+++++++++++++++
+++++++++++++++
+++++++++++++++
+++++++++++++++
+++++++++++++++
+++++++++++++++
+++++++++++++++
+++++++++++++++
+-+++++++++++-+
+++++++++++++++
========================================
Cancerous cell found at location (2,1)
Cancerous cell found at location (14,1)
Cancerous cell found at location (14,13)
========================================