Java 错误返回数组,用最后一项覆盖所有
我看到过一些类似的帖子,但不是我现在的问题。我要做的是调用一个函数,该函数显示编号为Java 错误返回数组,用最后一项覆盖所有,java,arrays,return,Java,Arrays,Return,我看到过一些类似的帖子,但不是我现在的问题。我要做的是调用一个函数,该函数显示编号为 1- File1.txt, 2- File2.txt .. 这正如预期的那样有效。问题是我需要返回另一个数组中这些文件的路径。当ISystem.out.print(arrayRutasFicheros[j])infor时,它会正确显示所有路径。但是当我试图从其他函数访问arrayRutasFicheros[j]时。它只会覆盖所有路径,并显示最后一条路径 public static String[] lista
1- File1.txt,
2- File2.txt
..
这正如预期的那样有效。问题是我需要返回另一个数组中这些文件的路径。当ISystem.out.print(arrayRutasFicheros[j])
infor
时,它会正确显示所有路径。但是当我试图从其他函数访问arrayRutasFicheros[j]
时。它只会覆盖所有路径,并显示最后一条路径
public static String[] listarArchivos() throws IOException{
File[] listadoDeFiles = documento.listFiles();
File[] arrayFicheros = null;
String[] arrayRutasFicheros = null;
if(documento.exists() ) {
for (int k=0; k< listadoDeFiles.length ;k++) {
File ficheroRuta = listadoDeFiles[k];
File fichero = new File(documento.getPath() + sep+ ficheroRuta.getName());
if(fichero.isFile()==true) {
arrayFicheros =new File[] {fichero};
System.out.println( k + " - " + ficheroRuta.getName());
for(int j= 0; j<arrayFicheros.length; j++) {
arrayRutasFicheros = new String[] {arrayFicheros[j].getPath()};
//here it works and it display all the path
System.out.println(arrayRutasFicheros[j]);
}
}
}
}
return arrayRutasFicheros;
}
public static muestraUnArchivo() throws IOException {
String [] Fichero =listarArchivos();
for(int k=0; k<Fichero.length; k++) {
//here just the last one
System.out.print(Fichero[k]);
}
}
输出:
-E:\Eclipse\Files\File3.txt
在循环的每次迭代中都会重新创建数组,该循环也位于长度为
1
的数组中。您需要一个动态结构来存储字符串路径,因为您事先不知道有多少路径。此外,您不需要一直创建单元素数组;使用列表
。大概
public static String[] listarArchivos() throws IOException {
List<String> al = new ArrayList<>();
if (documento.exists()) {
File[] listadoDeFiles = documento.listFiles();
for (File ficheroRuta : listadoDeFiles) {
File fichero = new File(documento.getPath()
+ sep + ficheroRuta.getName());
if (fichero.isFile()) {
al.add(fichero.getPath());
}
}
}
return al.toArray(new String[0]);
}
public静态字符串[]listarArchivos()引发IOException{
List al=新的ArrayList();
如果(documento.exists()){
文件[]listadoDeFiles=documento.listFiles();
用于(文件ficheroRuta:ListadoDistribles){
File fichero=新文件(documento.getPath())
+sep+ficherouta.getName());
if(fichero.isFile()){
al.add(fichero.getPath());
}
}
}
返回al.toArray(新字符串[0]);
}
下一行设置for结束迭代时的最后一个值
我希望你能理解其中的逻辑
arrayRutasFicheros = new String[] {arrayFicheros[j].getPath()};
如果您有任何疑问,请告诉我
快乐编码。以下几行不仅是不必要的,而且会导致您面临的问题。行,
arrayRutasFicheros=newstring[]{arrayFicheros[j].getPath()}
正在为k
的每个值重置arrayRutasFicheros
,因此您将只获得在k
的最后一个值处分配给它的值
if(fichero.isFile()==true) {
arrayFicheros =new File[] {fichero};
System.out.println( k + " - " + ficheroRuta.getName());
for(int j= 0; j<arrayFicheros.length; j++) {
arrayRutasFicheros = new String[] {arrayFicheros[j].getPath()};
//here it works and it display all the path
System.out.println(arrayRutasFicheros[j]);
}
}
如果有任何疑问/问题,请随时发表评论。只需逐步浏览代码(或输入一些日志)。您可以在每次
j
迭代中设置arrayRutasFicheros
。arrayRutasFicheros=newstring[]{arrayFicheros[j].getPath()}
使用一个条目创建一个数组(arrayFicheros[j]
条目)。因此,上一次迭代将其设置为arrayFicheros
中的最后一个元素-如果要复制arrayFicheros
路径,则应在循环之前分配一个与arrayFicheros
大小相同的arrayFicheros
。当然arrayFicheros
也只是一个元素。也许你想要一个列表
。谢谢大家的快速回答,你帮了很多忙!!!事实上,问题是我在每次迭代中都覆盖了变量。创建一个列表解决了它!这可能需要我比其他人更长的时间,毕竟我是个彻头彻尾的傻瓜:P谢谢你的回答!
if(fichero.isFile()==true) {
arrayFicheros =new File[] {fichero};
System.out.println( k + " - " + ficheroRuta.getName());
for(int j= 0; j<arrayFicheros.length; j++) {
arrayRutasFicheros = new String[] {arrayFicheros[j].getPath()};
//here it works and it display all the path
System.out.println(arrayRutasFicheros[j]);
}
}
if(fichero.isFile()) {
arrayRutasFicheros[k] = fichero.getPath();
}