user3119757
user3119757

Reputation: 5

How to write a method for multidimensional array

I want to write a method for selecting a specific year (say 2009) from the Time array.

This would refer (8001) which is basically a key for year 2009.

In the FactTable I would like to add all the figures which represent quantity (the 1s). So it would show sales total for year 2009. Anyone have a clue how i can do this.

The Time array stores {timekey, year, month, week, day}

The FactTable stores {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); 

Upvotes: 0

Views: 96

Answers (5)

Mr_and_Mrs_D
Mr_and_Mrs_D

Reputation: 34036

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
    }
}

Although I really do not get what are those arrays for

Upvotes: 1

Mitesh Pathak
Mitesh Pathak

Reputation: 1211

I hope this helps

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);

Upvotes: 0

Taylor Hx
Taylor Hx

Reputation: 2833

Array indices are accessed by making a call like so:

Object indexObject = array[index];

You indicate the index, surrounded by square-brackets, immediately following the name of the array.

You code should look like below:

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.
    }

}

Accessing the indices of multidimensional arrays is not different that accessing the indices of a single-dimension array since the first index of a multidimensional array will return another array with one less dimension. To demonstrate:

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.

To add together multiple indices of an array, an operation is performed like below:

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];

Aside from the lesson on how to access arrays, JosefN's answer is likely the solution to your problem.

Upvotes: 0

Nilsaw
Nilsaw

Reputation: 103

You can probably do this: A simplified solution for finding the sum of the values,

int sum=0
for (int i = 0; i< FactTable.length; i++){

         if(FactTable[i][3]==8001){
              sum+=FactTable[i][4]
            }        

}

Upvotes: 0

JosefN
JosefN

Reputation: 952

following code shows how to address fields in multidimensional array

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);

please note correction of second expression in for loop

Upvotes: 2

Related Questions