我想写一个从Time
数组中选择特定年份(比如说2009年)的方法。
这将引用(8001),这基本上是2009年的关键。
在FactTable
我想添加所有代表数量的数字(1s)。所以它会显示2009年的销售总额。任何人都知道如何做到这一点。
Time
数组存储{timekey, year, month, week, day}
FactTable
商店{clientid, Vehicleid, branchid, timekey, quantity, profit}
int[][] Time = new int[][]
{{8001,2009,1,1,1},
{8002,2010,1,1,7},
{8003,2011,1,1,5},
{8004,2012,1,1,5}};
int[][] FactTable = new int [][]
{{1,125,20,8001,1,2000},
{2,126,40,8001,1,1500},
{3,127,50,8001,1,2500},
{4,128,10,8001,1,2500}};
int sum = 0;
int year = 8001;
for (int i = 0; i< FactTable.length-1; i++)
{
for (int j = 1; j < FactTable[i].length-1; j++)
{
year = year + FactTable + [0][4];
}
}
System.out.println (year);
答案 0 :(得分:2)
以下代码显示了如何处理多维数组中的字段
int sum = 0;
int year = 8001;
for (int i = 0; i< FactTable.length; i++)
{
if (FactTable[i][3] == year) {
sum = sum + FactTable[i][4];
}
}
System.out.println(sum);
请注意for循环中第二个表达式的修正
答案 1 :(得分:1)
public class Test1 {
public static void main(String[] args) {
int[][] timeTable = new int[][] { { 8001, 2009, 1, 1, 1 },
{ 8002, 2010, 1, 1, 7 }, { 8003, 2011, 1, 1, 5 },
{ 8004, 2012, 1, 1, 5 } };
int[][] factTable = new int[][] { { 1, 125, 20, 8001, 1, 2000 },
{ 2, 126, 40, 8001, 1, 1500 }, { 3, 127, 50, 8001, 1, 2500 },
{ 4, 128, 10, 8001, 1, 2500 } };
meth(factTable, 2009, timeTable);
}
private static void meth(int[][] factTable, int year, int[][] timeTable) {
int timeKey = -1;
// find the timeKey from the year
for (int[] is : timeTable) { // for each row in timeTable
if (is[1] == year) { // if the year column (duh) is the year
timeKey = is[0]; // get the timekey
break; // we're done
}
}
if (timeKey == -1) return; //timeKey still -1: no timeKey for year
int sum = 0;
for (int[] js : factTable) { // for each row in factTable
if (js[3] == timeKey) { // if the timeKey column is right
sum += js[4]; // add to the sum the quantity
}
}
System.out.println(sum); // done
}
}
虽然我真的没有得到那些
的数组答案 2 :(得分:0)
通过这样调用来访问数组索引:
Object indexObject = array[index];
您可以在数组名称后面紧跟在方括号旁边指示索引。
您的代码应如下所示:
for (int i = 0; i< FactTable.length-1; i++)
{
for (int j = 1; j < FactTable[i].length-1; j++)
{
year = year + FactTable[0][4]; //This is how you access indices of an array.
}
}
访问多维数组的索引与访问单维数组的索引没有什么不同,因为多维数组的第一个索引将返回另一个维度较少的数组。为了证明:
Object[][][] array3 = new Object[1][1][1]; //This is a three dimensional array.
Object[][] array2 = array3[0]; //array3[0] returns a two dimensional array.
Object[] array1 = array3[0][0]; //array3[0][0] returns a one dimensional array.
要将数组的多个索引加在一起,将执行如下操作:
int a = array[0][4];
int b = array[1][4];
int c = array[2][4];
int d = array[3][4];
int result;
//The two operations below are functionally the same.
result = a + b + c + d;
result = array[0][4] + array[1][4] + array[2][4] + array[3][4];
除了有关如何访问数组的课程外,JosefN's answer可能是您问题的解决方案。
答案 3 :(得分:0)
你可以这样做: 用于查找值总和的简化解决方案
int sum=0
for (int i = 0; i< FactTable.length; i++){
if(FactTable[i][3]==8001){
sum+=FactTable[i][4]
}
}
答案 4 :(得分:0)
我希望这会有所帮助
int year = 2009;
int timeKey = -1;
for (int array[] : Time) {
if (array[1] == year) {
timeKey = array[0];
break;
}
}
int sum = 0;
for (int array[] : FactTable) {
if (array[3] == timeKey) {
sum += array[4];
}
}
System.out.println(sum);