Connecting Tech Pros Worldwide Forums | Help | Site Map

Problem in assigning values to 2-dimensional arrays...

Member
 
Join Date: Oct 2007
Posts: 38
#1: Dec 3 '07
Hello,

I'm doing a project in java and im not able to assign the values which are got from for loop to the 2-dimensional array.
Can anyone tell me how to assign the values and print them?

the code is:
Expand|Select|Wrap|Line Numbers
  1.                            gr.getGridArray();
  2.                for (int s=0;s<=NoOfElevations;s++)
  3.                    for (int t=0;t<=NoOfElevations;t++)
  4.                    {
  5.                             temp[][] = ;
  6.                             System.out.println("values are " + temp[s][t]);
  7.                    }
  8.            } 
  9.  
How will i assign s and t values to temp array?

Thanks in anticipation.......

Member
 
Join Date: Nov 2006
Posts: 94
#2: Dec 3 '07

re: Problem in assigning values to 2-dimensional arrays...


Quote:

Originally Posted by sumuka

Hello,

I'm doing a project in java and im not able to assign the values which are got from for loop to the 2-dimensional array.
Can anyone tell me how to assign the values and print them?

the code is:

Expand|Select|Wrap|Line Numbers
  1.                            gr.getGridArray();
  2.                for (int s=0;s<=NoOfElevations;s++)
  3.                    for (int t=0;t<=NoOfElevations;t++)
  4.                    {
  5.                             temp[][] = ;
  6.                             System.out.println("values are " + temp[s][t]);
  7.                    }
  8.            } 
  9.  
How will i assign s and t values to temp array?

Thanks in anticipation.......

I havnt worked with 2d arrays much but line 5 looks like it has a problem.
Member
 
Join Date: Nov 2007
Location: ZIMBABWE
Posts: 118
#3: Dec 3 '07

re: Problem in assigning values to 2-dimensional arrays...


Quote:

Originally Posted by sumuka

Hello,

I'm doing a project in java and im not able to assign the values which are got from for loop to the 2-dimensional array.
Can anyone tell me how to assign the values and print them?

the code is:

Expand|Select|Wrap|Line Numbers
  1.                            gr.getGridArray();
  2.                for (int s=0;s<=NoOfElevations;s++)
  3.                    for (int t=0;t<=NoOfElevations;t++)
  4.                    {
  5.                             temp[s][t] =someValue ;
  6.                             System.out.println("values are " + temp[s][t]);
  7.                    }
  8.            } 
  9.  
How will i assign s and t values to temp array?

Thanks in anticipation.......

You assign someValue to the array at [s][t] . Is your problem solved?
Member
 
Join Date: Oct 2007
Posts: 38
#4: Dec 3 '07

re: Problem in assigning values to 2-dimensional arrays...


Quote:

Originally Posted by heat84

You assign someValue to the array at [s][t] . Is your problem solved?

Thanks for replying .
But i need to assign the values of s and t which i've used in the for loop .
I need to assign the values of s and t to this array and then display.

Please help....
JosAH's Avatar
Expert
 
Join Date: Mar 2007
Posts: 10,611
#5: Dec 3 '07

re: Problem in assigning values to 2-dimensional arrays...


Quote:

Originally Posted by sumuka

Thanks for replying .
But i need to assign the values of s and t which i've used in the for loop .
I need to assign the values of s and t to this array and then display.

Please help....

Variables s and t are *two* values; a single cell in a two dimensional array can
just contain *one* single value.

kind regards,

Jos
Member
 
Join Date: Oct 2007
Posts: 52
#6: Dec 3 '07

re: Problem in assigning values to 2-dimensional arrays...


Quote:

Originally Posted by sumuka

Thanks for replying .
But i need to assign the values of s and t which i've used in the for loop .
I need to assign the values of s and t to this array and then display.

Please help....

dude, s&t are not the values of the array but index's, two dimentional array has pair of number to form a specific index of theh array value, for example arr[1][0] is the value at second horizontal line but first in the vertical line of you dimenssions, do you want the index(position) of the value or do you want the value? ...
Member
 
Join Date: Nov 2007
Location: ZIMBABWE
Posts: 118
#7: Dec 3 '07

re: Problem in assigning values to 2-dimensional arrays...


Quote:

Originally Posted by sumuka

Thanks for replying .
But i need to assign the values of s and t which i've used in the for loop .
I need to assign the values of s and t to this array and then display.

Please help....

Ok , you can create an object e.g MyValues that that contains the s and t as instance variables.
for example
Expand|Select|Wrap|Line Numbers
  1.  
  2.    1.
  3.       gr.getGridArray();
  4.    2.
  5.                      for (int s=0;s<=NoOfElevations;s++)
  6.    3.
  7.                          for (int t=0;t<=NoOfElevations;t++)
  8.    4.
  9.                          {
  10.                     MyValues someValue=new MyValues(s,t);
  11.    5.
  12.                                   temp[s][t] =someValue ;
  13.    6.
  14.                                   System.out.println("values are " + temp[s][t]);
  15.    7.
  16.                          }
  17.    8.
  18.                  }
  19.  
In this case the tempArray then will have to be of MyValues type.

Hope this is helpful.....
Reply