473,401 Members | 2,127 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,401 software developers and data experts.

how to loop though cols?

10
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 1179
chaarmann
785 Expert 512MB
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
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 Expert 512MB
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
by: Aaron | last post by:
Is there a way I can loop through everything that is in session?
4
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
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
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
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
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
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
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
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
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
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
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
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...
0
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...

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.