How may I accomplish the task above? What I have been trying to do is separate the pages into different arrays but failed terribly.
我怎样才能完成上述任务?我一直在尝试做的是将页面分成不同的数组,但失败得非常糟糕。
Code by request (does not even close to work)
按要求编码(甚至不接近工作)
int a = 1; int b = 5;
File folder = new File("c:/files");
File[] listOfFiles = folder.listFiles();
String[] page1 = new String[7];
String[] page2 = new String[7];
String[] page3 = new String[7];
String[] page4 = new String[7];
String[] page5 = new String[7];
String[] page6 = new String[7];
int c = 0;
for (int i = 0;i<listOfFiles.length; i++)
{
if(i>=0 && i <= 7)
{
page1[i] = listOfFiles[i].getName();
}
else if(i>=8 && i<=15)
{
page2[i] = listOfFiles[i].getName();
}
else if(i>=16 && i<=23)
{
page3[i] = listOfFiles[i].getName();
}
else if(i>=24 && i<=31)
{
page4[i] = listOfFiles[i].getName();
}
else if(i>=32 && i<=39)
{
page5[i] = listOfFiles[i].getName();
}
}
2 个解决方案
#1
1
This might help you:
这可能对您有所帮助:
- Read text file in java
- 在java中读取文本文件
- Use the modulo operator
- 使用模运算符
#2
0
Your code can't work, because when you do page2[i] = xxx
, the variable named i
should take a value between 0 and 6, and its value goes from 8 to 15. The same for the other pages. Even page1 will fail because the last index you use is 7, that is not in the range 0-6.
您的代码无法工作,因为当您执行page2 [i] = xxx时,名为i的变量应取0到6之间的值,其值从8到15.其他页面也是如此。即使page1也会失败,因为你使用的最后一个索引是7,它不在0-6范围内。
Try something like:
尝试以下方法:
for (int i=0; i<listOfFiles.length; i++)
{
if(i>=0 && i<7)
{
page1[i] = listOfFiles[i].getName();
}
else if(i>=7 && i<15)
{
page2[i-7] = listOfFiles[i].getName();
}
else if(i>=16 && i<23)
{
page3[i-16] = listOfFiles[i].getName();
}
...
}
I hope you get the idea. Even this way your code is a little ugly to maintain, you should think about how to define only one bidimensional array, and to use the modulus (%
) operator to access it in a simpler way and with less code.
我希望你明白这个主意。即使这样你的代码维护起来有点难看,你应该考虑如何只定义一个二维数组,并使用模数(%)运算符以更简单的方式和更少的代码访问它。
The following code is untested, but I hope it guides you:
以下代码未经测试,但我希望它能指导您:
int NUM_PAGES = 7;
File folder = new File("c:/files");
File[] listOfFiles = folder.listFiles();
String[][] pages = new String[NUM_PAGES][listOfFiles.length/NUMPAGES];
for (int i=0; i<listOfFiles.length; i++) {
int currentPage = i % 7;
int currentPosition = pages[currentPage].length;
pages[currentPage][currentPosition] = listOfFiles[i].getName();
}
You can also use Java Collections that would simplify your code even more;
您还可以使用Java集合来进一步简化代码;
#1
1
This might help you:
这可能对您有所帮助:
- Read text file in java
- 在java中读取文本文件
- Use the modulo operator
- 使用模运算符
#2
0
Your code can't work, because when you do page2[i] = xxx
, the variable named i
should take a value between 0 and 6, and its value goes from 8 to 15. The same for the other pages. Even page1 will fail because the last index you use is 7, that is not in the range 0-6.
您的代码无法工作,因为当您执行page2 [i] = xxx时,名为i的变量应取0到6之间的值,其值从8到15.其他页面也是如此。即使page1也会失败,因为你使用的最后一个索引是7,它不在0-6范围内。
Try something like:
尝试以下方法:
for (int i=0; i<listOfFiles.length; i++)
{
if(i>=0 && i<7)
{
page1[i] = listOfFiles[i].getName();
}
else if(i>=7 && i<15)
{
page2[i-7] = listOfFiles[i].getName();
}
else if(i>=16 && i<23)
{
page3[i-16] = listOfFiles[i].getName();
}
...
}
I hope you get the idea. Even this way your code is a little ugly to maintain, you should think about how to define only one bidimensional array, and to use the modulus (%
) operator to access it in a simpler way and with less code.
我希望你明白这个主意。即使这样你的代码维护起来有点难看,你应该考虑如何只定义一个二维数组,并使用模数(%)运算符以更简单的方式和更少的代码访问它。
The following code is untested, but I hope it guides you:
以下代码未经测试,但我希望它能指导您:
int NUM_PAGES = 7;
File folder = new File("c:/files");
File[] listOfFiles = folder.listFiles();
String[][] pages = new String[NUM_PAGES][listOfFiles.length/NUMPAGES];
for (int i=0; i<listOfFiles.length; i++) {
int currentPage = i % 7;
int currentPosition = pages[currentPage].length;
pages[currentPage][currentPosition] = listOfFiles[i].getName();
}
You can also use Java Collections that would simplify your code even more;
您还可以使用Java集合来进一步简化代码;