我再一次把我的大脑作为错误信息 不断涌来。我正在学习如何使用数组 - 常规和多维。我遇到问题a)用销售数据填充数组,还有一个部分,我得到“无法将字符串转换为int”。当我修改这个数组以获得一个字符串值时, - 然后我得到一个翻转错误, - “无法将int转换为String。感谢任何帮助。谢谢。
public class Sales{
public static void main (String []Args)
{
//a single dimension array containing the following customer names
String [] Names = {"John Doe","Pete BuysAlot","Joe Stingy","Mary LikesOurStuff" } ;
// for(int 0;i<Names.length; i++)
// System.out.printl=n(Names[i]);}
//a single dimension array containing the names of //each month
String[]Months= new String [11];
Months[0] = " Jan ";
Months[1] = " Feb ";
Months[2] = " Mar ";
Months[3] = " Apr ";
Months[4] = " May ";
Months[5] =" June ";
Months[6] =" July ";
Months[7] =" Aug ";
Months[8] =" Sept ";
Months[9] =" Oct ";
Months[10]=" Nov ";
Months[11]=" Dec ";
// this next section creates the variables and data to create and initialize
// a two dimension array that reflects the purchases each person made per month.
//It will have the initial data in the following table
int[][]slsData = { {200,50,30,300,155,220,80,70,95,110,3,400},
{ 1200,2000,1500,900,1300,800,750,500,900,1200,1500,2000},
{10,0,0,20,5,30,10,0,0,10,15,0},
{500,100,200,400,600,200,150,155,220,336,43 ,455}
};
String [][] slsTablePP = slsData[3][Months]; //here is where an error occurance is. [months] is a declared as a new string array but errors.
{
for (int row = 0; row <Names.length; row++)
for (int col = 0; col<Months.length; col++)
System.out.println(slsTablePP[row][col]); }
// array to hold sales figures totals by month
for( int x=0;x<mthlySales-1;x++)
System.out.println(Names[i] + mthlySls[x]);
}
}
}
答案 0 :(得分:0)
slsData[3][Months]
Months
在这里应该是一个整数,以访问slsData
中第3个数组的第n个元素。但Months
的类型为String[]
,因此没有意义。也许你要传递的是Months
数组的长度。在这种情况下,您将使用slsData[3][Months.length]
。
但是因为slsData
是一个int数组的数组,slsData[3][12]
就是一个int。因此,您无法将其分配给String[][]
类型的变量。
请注意,您应该遵守Java命名约定。变量应该有有意义的名称,由单词组成(slsData
和slsTablePP
是什么意思?),并且应该以小写字母开头(即months
,而不是Months
)。
答案 1 :(得分:0)
String [][] slsTablePP = slsData[3][Months];
月份不是整数。 您不能直接将一个int数组转换为String数组。 要将整数数组转换为字符串数组,请查看Converting an int array to a String array
String[]Months= new String [11];
上述数组的大小应为12。
答案 2 :(得分:0)
Months
只是String数组的名称。为了访问数组中的各个值,您必须使用数组运算符[]
将所需值传递给它。
例如:
String[] stringArray = new String[4];
String fourthString = stringArray[3];
或
String[] stringArray = new String[someList.size()]
for (int i=0; i<stringArray.length; i++) {
stringArray[i] = someList.get(i);
//some other stuff maybe
}
答案 3 :(得分:0)
显然,这一行是错误的:
String [][] slsTablePP = slsData[3][Months];
数组是类型的,你无法将int[][]
神奇地转换为String[][]
,而且,在Java中,数组始终由int
索引。其中slsData[3][Months]
Months
类型为String[][]
的{{1}}没有任何意义
请注意,在某些语言中,如Python,您有关联数组(Map
和Java中的派生类),它们可以具有数组语法,其中字符串作为索引,例如
# python syntax
salesByMonth = {}
salesByMonth["jan"] = 2000;
salesByMonth["feb"] = 500;
声明Months
数组时还有另一个问题。在
String[] Months= new String [11]; // <= should be 12 !
值[11]
是数组的大小,而不是最后一个索引,因此它应该是12
。在Java数组中,0
已编入索引。
下面我举一个注释示例,说明如何匹配不同的数据数组。
public class Sales{
public static void main (String []Args) {
//a single dimension array containing the following customer names
String [] Names = {"John Doe","Pete BuysAlot","Joe Stingy","Mary LikesOurStuff" } ;
//a single dimension array containing the names of each month
String[]Months= new String [12]; // <= 12 is the size not the last index !
Months[0] = " Jan "; Months[1] = " Feb ";
Months[2] = " Mar "; Months[3] = " Apr ";
Months[4] = " May "; Months[5] = " June ";
Months[6] = " July "; Months[7] = " Aug ";
Months[8] = " Sept "; Months[9] = " Oct ";
Months[10]= " Nov "; Months[11]= " Dec ";
// two dimension array that reflects the purchases each person made per month.
int[][]slsData = {
{ 200, 50, 30, 300, 155, 220, 80, 70, 95, 110, 3, 400},
{1200, 2000, 1500, 900, 1300, 800, 750, 500, 900, 1200, 1500, 2000},
{ 10, 0, 0, 20, 5, 30, 10, 0, 0, 10, 15, 0},
{ 500, 100, 200, 400, 600, 200, 150, 155, 220, 336, 43 , 455}
};
// match the sales data with name & month through the _indexes_
// in the arrays. Here iName [0, 3] and iMonth [0, 11] will
// give the sales amount for a (name, month): slsData[iName][iMonth]
for (int iName = 0; iName < Names.length; iName++) {
System.out.println("\nSales for "+Names[iName]);
int total = 0; // total (re)set to 0 for each name
for (int iMonth = 0; iMonth < Months.length; iMonth++) {
System.out.println(Months[iMonth] + slsData[iName][iMonth]);
total += slsData[iName][iMonth];
}
System.out.println("Total sales for "+Names[iName] + ": "+total);
}
}
}