473,498 Members | 2,021 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

how to loop though cols?

10 New Member
How can I set up my nested for loop so it loop though columns and not rows?

I want to get min/Max value of a all column in a two-dimensional arra

so for example, lets say you have the following 2d array:

1 2 3
4 5 6
7 8 9

Than the out put should be:

-- col 1 --
min value at col 1 is 1
max value at col 1 is 7
-- col 2 --
min value at col 2 is 2
max value at col 2 is 8
-- col 3 --
min value at col 3 is 3
max value at col 3 is 9

Expand|Select|Wrap|Line Numbers
  1. public static void col(int[][] array) {
  2.  
  3.         for (int i = 0; i < array.length; i++) {
  4.             int minValue = 0;
  5.             int maxValue = 0;
  6.             for (int j = 0; j < array[i].length; j++) {
  7.                 if (minValue > array[j][i]) {
  8.                     minValue = array[j][i];
  9.                 }
  10.                 if (maxValue < array[j][i]) {
  11.                     maxValue = array[j][i];
  12.                 }
  13.             }
  14.         }
  15.  
  16.     }
  17.  
error:

Expand|Select|Wrap|Line Numbers
  1. Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 3
Sep 9 '15 #1
3 1182
chaarmann
785 Recognized Expert Contributor
having a quick look, I must say, your code will work perfectly when you switch the column and row variables in line 7 to 12.
Your array should be accessed by "array[i][j]" and not by "array[j][i]".

Your code can be optimized. Just store "array[i][j]" in a local variable called "currentValue" and use that instead of accessing the array again and again.

For better readability, consider using Math.max() and Math.min() methods instead of if() statements.
Sep 9 '15 #2
gamer2d
10 New Member
but if I do some thing like this below, wont that loop though rows and not cols? I am trying to find min of each cols.


Expand|Select|Wrap|Line Numbers
  1. public static void col(int[][] array) {
  2.         for (int i = 0; i < array.length; i++) {
  3.             int minValue = 0;
  4.             int maxValue = 0;
  5.             for (int j = 0; j < array[i].length; j++) {
  6.                 if (minValue > array[i][j]) {
  7.                     minValue = array[i][j];
  8.                 }
  9.             }
  10.         }
  11.  
  12.     }
Sep 9 '15 #3
chaarmann
785 Recognized Expert Contributor
Ok, you are right. But it is tricky to loop the other way around. because the size of each array can be different. For example you can have an array like this:
Expand|Select|Wrap|Line Numbers
  1. int[][] array = {{1,2}, {4,5,6}, {7}};
So how do you know how many columns you have? The first subarray has 2, the second 3 and the third 1.
You can try to do get the maximum in your double loop somehow if you need to optimize performance, but it is cleaner code to do it separately.
So following plan
1. Step: Make a look that gets the maximum array size of all subarrays
2. Step: make another loop that only gets the first entry of each subarray (hard-coded index 0), that is the first column, and remembers the maximum value of all these entries. Remember that there could be a subarray of size 0, so check the size of the array first before accessing the entry and skip it if the array size is lesser than 1.
3. step: now we want to do it for all columns: just surround the code in step 2 with a loop that increases the counter-variable "c" (c=column) each time until the maximum computed in step 1 is reached. Then replace all hardcoded index "0" in step 2 with "c". Adapt your array size check accordingly (hardcoded 1 --> c+1)

I want you to do the steps, else you won't learn. If you have difficulties with one of these steps, then tell me and I will help you further on or just spoonfeed you (that means give you "teh codez")
Sep 10 '15 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

11
4725
by: Aaron | last post by:
Is there a way I can loop through everything that is in session?
4
3456
by: Ryan Liu | last post by:
How to loop though controls on the form? I set each control's TabStop and TabIndex, but at run time, I press Tab key, it jump though first control to last control and does not go back to the...
2
2895
by: Peter | last post by:
Hi I have an application that opens a new form displaying an alert for every new alert the system receives. The code is running on a timer in a separate thread and all the alert forms are...
6
2902
by: Stuart | last post by:
In VB6 I could cycle though loaded forms like this. Dim MyForm as Form For each MyForm in Forms debug.print MyForm.Name Next How can I do it in VB.NET?
2
1250
by: Mike Walters | last post by:
I am trying to loop thought all the services on a system and stat/stop then as needed, I know How I can start or stop then, but how do I loop thought all installed services. Thanks Mike
2
3343
by: Ryan Liu | last post by:
Hi, I heard foreach is slower then use loop directly, is that really true? When loop though a list in a multiple thread environment, is that a good practice to copy this list to an array the...
1
1904
by: game2d | last post by:
so i have two animation. one is when player is standing and one is when player get hit. 'standing' animation will keep looping for ever. and 'planehit' animation should loop though onces and stop. ...
1
1415
by: game2d | last post by:
i have a enemy and spriteanimation classes. in Enemy class i have bufferedimage array with 4 different images. than i am creating a animation and storing in 'animationEnemyHit' variable. than i am...
17
2592
by: yashuaking | last post by:
I have a form called InvestmentF based on a table InvestmentF with the following fields Investor Name InvestorID InvestmentDate MaturityDate What I want to is that on load, Access should...
0
7162
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7197
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
7375
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
1
4899
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4584
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3088
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3078
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1411
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
650
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.