Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

two dimesnional Array to arraylist

Posted on 2012-03-23
4
Medium Priority
?
358 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 2000 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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

A solution for Fortify Path Manipulation.
This article will show how Aten was able to supply easy management and control for Artear's video walls and wide range display configurations of their newsroom.
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
Introduction to Processes
Suggested Courses

971 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