Java 递归方法,该方法需要访问中断的数组
我试图创建一种方法,在不中断子目录格式的情况下调整特定文件夹中所有图像的大小。我有两个整数数组,用于指定要调整大小的图像的宽度和高度。我对该方法使用递归调用来迭代该文件夹及其子目录 问题是,每当我使用递归调用时,它都从宽度和高度数组的开头开始,而我希望它从停止的地方开始。我该如何解决这个问题Java 递归方法,该方法需要访问中断的数组,java,arrays,recursion,Java,Arrays,Recursion,我试图创建一种方法,在不中断子目录格式的情况下调整特定文件夹中所有图像的大小。我有两个整数数组,用于指定要调整大小的图像的宽度和高度。我对该方法使用递归调用来迭代该文件夹及其子目录 问题是,每当我使用递归调用时,它都从宽度和高度数组的开头开始,而我希望它从停止的地方开始。我该如何解决这个问题 resizeFiles(File[] folderToResize, String inputImagePath, int backgroundColor) throws IOException {
resizeFiles(File[] folderToResize, String inputImagePath, int backgroundColor) throws IOException {
int width[] = {36, 48, 72, 96, 96, 48, 72, 36, 48, 72, 96, 128, 48, 50, 80, 80, 1024, 40, 114, 57, 58, 60, 120, 180, 144, 72, 144, 76, 152, 87, 57, 114, 72, 144, 114, 57, 144, 72, 180, 128, 64, 173, 48, 62, 180, 800, 480, 320, 200, 480, 320, 1280, 720, 480, 320, 480, 240, 225, 2008, 1024, 1536, 1536, 768, 768, 960, 480, 640, 640, 640, 640, 320, 320, 64, 480};
int height[] = {36, 48, 72, 96, 96, 48, 72, 36,48, 72, 96, 128, 48, 50, 80, 80, 1024, 40, 114, 57, 58, 60, 120, 180, 144, 72, 144, 76, 152, 87, 57, 114, 72, 144, 114, 57, 144, 72, 180, 128, 64, 173, 48, 62, 180, 800, 480, 320, 200, 480, 320, 1280, 720, 480, 320, 480, 240, 225, 1536, 783, 2008, 2008, 1004, 1004, 640, 320, 960, 960, 1136, 1136, 480, 480, 64, 800};
for (int i = 0; i < folderToResize.length; i++) {
if (folderToResize[i].isDirectory()) {
resizeFiles(folderToResize[i].listFiles(), inputImagePath, backgroundColor);
} else {
System.out.println("File Pathway: " + folderToResize[i].getAbsolutePath() + " Width: " + width[i] + " Height: " + height[i]);
resize(inputImagePath, folderToResize[i].getAbsolutePath(), width[i], height[i], backgroundColor);
}
}
}
resizeFiles(文件[]folderstoresize,字符串inputImagePath,int backgroundColor)引发IOException{
整数宽度[]={36, 48, 72, 96, 96, 48, 72, 36, 48, 72, 96, 128, 48, 50, 80, 80, 1024, 40, 114, 57, 58, 60, 120, 180, 144, 72, 144, 76, 152, 87, 57, 114, 72, 144, 114, 57, 144, 72, 180, 128, 64, 173, 48, 62, 180, 800, 480, 320, 200, 480, 320, 1280, 720, 480, 320, 480, 240, 225, 2008, 1024, 1536, 1536, 768, 768, 960, 480, 640, 640, 640, 640, 320, 320, 64, 480};
整数高度[]={36, 48, 72, 96, 96, 48, 72, 36,48, 72, 96, 128, 48, 50, 80, 80, 1024, 40, 114, 57, 58, 60, 120, 180, 144, 72, 144, 76, 152, 87, 57, 114, 72, 144, 114, 57, 144, 72, 180, 128, 64, 173, 48, 62, 180, 800, 480, 320, 200, 480, 320, 1280, 720, 480, 320, 480, 240, 225, 1536, 783, 2008, 2008, 1004, 1004, 640, 320, 960, 960, 1136, 1136, 480, 480, 64, 800};
对于(int i=0;i
编辑:
我把我的代码改成了这个,但是现在它不能正确地通过目录。奇怪的是,我没有改变它通过目录的方式。任何帮助都将不胜感激
resizeFiles(File[] folderToResize, String inputImagePath, int backgroundColor, int width[], int height[], int previousIndex) throws IOException { //won't walk through the directories properly
for (int i = 0; i < folderToResize.length; i++) {
if (folderToResize[i].isDirectory()) {
resizeFiles(folderToResize[i].listFiles(), inputImagePath, backgroundColor, width, height, i);
} else {
System.out.println("File Pathway: " + folderToResize[i].getAbsolutePath() + " Width: " + width[previousIndex] + " Height: " + height[previousIndex] + " Index: " + previousIndex);
resize(inputImagePath, folderToResize[i].getAbsolutePath(), width[previousIndex], height[previousIndex], backgroundColor);
break;
}
}
}
resizeFiles(File[]folderstoresize,String inputImagePath,int backgroundColor,int width[],int height[],int previousIndex)抛出IOException{//无法正确遍历目录
for(int i=0;i
数组中的图像大小与文件之间的关系是什么?如果数组中的文件比条目多怎么办?在递归方法中添加一个参数,该参数包含上次方法调用中的上一个索引。即)调整文件大小(文件[]a、字符串b、int c、int previousIndex)@JosephD我尝试了这个,然后添加了一个previousIndex=I;就在print语句之后,它仍然不能正常工作,还有其他建议吗?