Solved

two dimesnional Array to arraylist

Posted on 2012-03-23
4
342 Views
Last Modified: 2012-06-27
I wrote the below program and I got an output with which I don't want...


public class test {
public static void main(String args[]){
      int[][] providerAndYr = new int[15][2];
      int idx = 0;      
      int providerId = 0;
      int IncentiveYr = 0;
      for(int i =0; i<8; i++){
            providerAndYr[idx][0] = i;
            providerAndYr[idx++][1] = i+1;                                          
      }
      for(int j=0; j<providerAndYr.length; j++) {
            providerId = providerAndYr[j][0];
            IncentiveYr = providerAndYr[j][1];
            System.out.println("updating for provider=" + providerId);
      }      
}
}



output:
updating for provider=0
updating for provider=1
updating for provider=2
updating for provider=3
updating for provider=4
updating for provider=5
updating for provider=6
updating for provider=7
updating for provider=0
updating for provider=0
updating for provider=0
updating for provider=0
updating for provider=0
updating for provider=0
updating for provider=0

Question:

I want to avoid "updating for provider=0" I know I am getting those because I am using
for(int j=0; j<providerAndYr.length; j++) if I had used for(int j=0; j<idx; j++) my problem is solved but
I want to use the below part of code in another class and I don't want to pass temporary variable (idx) to it..is there any other way...to do it...

for(int j=0; j<providerAndYr.length; j++) {
            providerId = providerAndYr[j][0];
            IncentiveYr = providerAndYr[j][1];
            System.out.println("updating for provider=" + providerId);
      }
0
Comment
Question by:shragi
4 Comments
 

Author Comment

by:shragi
ID: 37757260
for(int i =0; i<8; i++){
            providerAndYr[idx][0] = i;
            providerAndYr[idx++][1] = i+1;                                          
      }


In this part of code the value 8 is not fixed...I used it as an example... to explain my problem .... it may vary if it is fixed i would have declared my array with size 8 instead of 8....
0
 
LVL 7

Expert Comment

by:gudii9
ID: 37757343
0
 
LVL 47

Accepted Solution

by:
for_yan earned 500 total points
ID: 37757424
sorry don't understadn your problem ;
if uyou want to avoid updating for provodeId 0, why not to skip it:

for(int j=0; j<providerAndYr.length; j++) {
            providerId = providerAndYr[j][0];
if(providerId == 0)continue;
            IncentiveYr = providerAndYr[j][1];
            System.out.println("updating for provider=" + providerId);
      }

Open in new window

0
 
LVL 86

Expert Comment

by:CEHJ
ID: 37757443
I know I am getting those because I am using
for(int j=0; j<providerAndYr.length; j++)

No - the reason you're getting that is that you only partially initialized 'providerAndYr' in the first loop by using a counter with a value lower than the length of the array
0

Featured Post

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This is an explanation of a simple data model to help parse a JSON feed
Entering a date in Microsoft Access can be tricky. A typo can cause month and day to be shuffled, entering the day only causes an error, as does entering, say, day 31 in June. This article shows how an inputmask supported by code can help the user a…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
The viewer will learn how to implement Singleton Design Pattern in Java.

776 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question