Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在Java中,我可以使数组的每个索引等于不同的字符串值吗?_Java_Arrays_For Loop - Fatal编程技术网

在Java中,我可以使数组的每个索引等于不同的字符串值吗?

在Java中,我可以使数组的每个索引等于不同的字符串值吗?,java,arrays,for-loop,Java,Arrays,For Loop,对于我正在制作的程序,我需要使文件数组的每个索引等于不同的字符串变量。我曾尝试使用for循环迭代每个索引,然后将其分配给不同的字符串变量,但没有成功 守则: final String user = System.getProperty("user.home"); final String OS = System.getProperty("os.name") if (System.getProperty("os.name").equals("Mac OS X")){ File fo

对于我正在制作的程序,我需要使
文件数组的每个索引
等于不同的
字符串
变量。我曾尝试使用for循环迭代每个索引,然后将其分配给不同的
字符串
变量,但没有成功

守则:

final String user = System.getProperty("user.home");
final String OS = System.getProperty("os.name")
if (System.getProperty("os.name").equals("Mac OS X")){
        File folder = new File(user+"/example");
        // if file doesn't exist, then create it
        if (!folder.exists()){
            folder.mkdir();
        }

        File[] listOfFiles = folder.listFiles();

        for (int i = 0; i < listOfFiles.length; i++) {
          if (listOfFiles[i].isFile()) {
            System.out.println(listOfFiles[i].getName());
          } else if (listOfFiles[i].getName().equals(".DS_Store")){
              listOfFiles[i].delete();
          }
        }  
    }
final String user=System.getProperty(“user.home”);
最后一个字符串OS=System.getProperty(“OS.name”)
if(System.getProperty(“os.name”).equals(“Mac os X”)){
文件夹=新文件(用户+“/示例”);
//如果文件不存在,则创建它
如果(!folder.exists()){
folder.mkdir();
}
File[]listOfFiles=folder.listFiles();
for(int i=0;i
如果您有一个
文件[]
并且希望为数组的每个索引分配一个
字符串,则有两个选项,因为对象数组只能保存其指定类的对象及其类的子类的对象

第一个选项是从
对象[]
开始,而不是从
文件[]
开始。这样,假设您有n个文件

Object[] filesThenStrings = new Object[n];

//Populate filesThenStrings with File objects here, 
//this is legal since all classes are a subclass of Object 
//and java does upcasting for you

for(int i = 0; i < n; i++) {
  filesThenStrings[i] = someString; //where this is the string you
                                    //want to replace the i-th file with
}
Object[]filesThenStrings=新对象[n];
//在此处使用文件对象填充文件字符串,
//这是合法的,因为所有类都是Object的子类
//java会为您进行升级
对于(int i=0;i
否则,你可以这样做。再次假设n个文件

File[] files = new File[n];

//populate here

String[] correspondStrings = new String[n];

for(int i = 0; i < n; i++) {
  correspondStrings[i] = someString; //where someString pertains to
                                     //files[i]
}
File[]files=新文件[n];
//在这里居住
字符串[]对应字符串=新字符串[n];
对于(int i=0;i
请提供您的代码,不要只描述它。这样,我们就更容易帮助您了。给每个索引分配不同的字符串值是什么意思。是否要在数组的每个索引处存储不同的
字符串
?如果已将数组初始化为存储
文件
,则无法在其中存储
字符串
。我建议使用
对象
数组,或者创建一个新的
字符串
数组,该数组与
文件
数组具有一对一的对应关系。一个文件数组的每个索引等于一个不同的字符串变量-嗯,它们是两种不同的类型。添加代码以便我能提供帮助you@TheLostMind,我想将文件名存储到字符串变量中谢谢!!你的回答真的很有帮助@官方的团队赌客没问题!