Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/316.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/neo4j/3.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 创建int数组的字符串数组时遇到问题_Java_Arrays_String_Int - Fatal编程技术网

Java 创建int数组的字符串数组时遇到问题

Java 创建int数组的字符串数组时遇到问题,java,arrays,string,int,Java,Arrays,String,Int,我需要创建一个包含在单词数组中的整数数组。我总是出错 无法从int[]转换为java.lang.String[] 上面写着 private String[][] expenseName = {clothes, tuition, transportation, food, housing, books}; 这是我的密码 public class Budget{ ///////////////fields//////////////// int expenseAmount[] = {1000

我需要创建一个包含在单词数组中的整数数组。我总是出错

无法从
int[]
转换为
java.lang.String[]

上面写着

private String[][] expenseName = {clothes, tuition, transportation, food, housing, books};
这是我的密码

public class Budget{

///////////////fields////////////////


int expenseAmount[] = {1000, 2000, 3000, 4000, 5000, 6000};
int monthNumber[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12};


int clothes[]= {100, 110, 120, 130, 140, 150, 160, 170, 180, 190, 200, 210};
int tuition[] = {200, 200, 200, 200, 200, 200, 200, 200, 200, 200, 200, 200};
int transportation[]={100, 110, 120, 130, 140, 150, 160, 170, 180, 190, 200, 210};
int food[]={80, 80, 80, 80, 80, 80, 80, 80, 80, 80, 80, 80};
int housing[]={150, 150, 150, 150, 150, 150, 150, 150, 150, 150, 150};
int books[]= {200, 0, 0, 0, 0, 0, 0, 300, 0, 0, 0, 0};
int i=0; // this is arbitrary. Never hard code numbers unless that number is never going to change. in that case you make a variable and define it.
int month_num;
private String[][] expenseName = {clothes, tuition, transportation, food, housing, books};

/////////constructors///////////////
public Budget() {}

public Budget(int name) {this.expenseName[i][i] = name;}

public Budget(String name, int clothes, int tuition, int transportation, int food, int housing, int books)
{
this.expenseName[i] = name;
this.clothes[i] = clothes;
this.tuition[i] = tuition;
this.transportation[i] = transportation;
this.food[i] = food;
this.housing[i] = housing;
this.books[i] = books;
this.monthNumber[i] = month_num;
}


/////////////methods///////////
public String getexpenseName() {return expenseName[i][i];}

public int getclothes() {return clothes[i];}//change the I
public int gettuition() {return tuition[i];}
public int gettransporation() {return transportation[i];}
public int getfood() {return food[i];}
public int gethousing() {return housing[i];}
public int books() {return books[i];}
public int getmonthNumber() {return monthNumber[i];}//change the i

public void setExpenseName(String name)
{
this.expenseName[i][i] = name;
}

public boolean setexpenseAmount(int num)
{
if (this.expenseAmount[i] == 0)
{this.expenseAmount[i] = num;
return true;
}
else
return false;

正如错误消息明确指出的那样,您不能将
int[]
放入
String[]
s的数组中。

您需要在字符串周围加引号:

private String[] expenseName = {"clothes", "tuition", "etc"};
或者您需要将其声明为int[][]

private int[][] expenseName = {clothes, tuition};
改变


在Java中,不能将int数组用作字符串数组

就像错误所说的那样,这是行不通的


我想你一定熟悉一些没有类型检查的语言,你可以做任何你想做的事情。但这是Java。

您必须将
int[]
转换为
String[]
,例如如下所示

public class ArrayIntToString {
public static void main(String[] args) {
    int[] numbers = { 1, 2, 3 };
    String[] numbersAsStrings = new String[numbers.length];
    int i = 0;
    for (Integer number : numbers) {
        numbersAsStrings[i++] = number.toString();
    }
}
}

如果数组是
int
,为什么要将其声明为
String
?(挠头)我不认为你的第一个片段是OP想要的。但即使是这样,您也有一个小小的错误:您声明了一个字符串数组,但只提供了一个字符串数组。
private int[][] expenseName = {clothes, tuition, transportation, food, housing, books};
public class ArrayIntToString {
public static void main(String[] args) {
    int[] numbers = { 1, 2, 3 };
    String[] numbersAsStrings = new String[numbers.length];
    int i = 0;
    for (Integer number : numbers) {
        numbersAsStrings[i++] = number.toString();
    }
}
}