?
Solved

two dimesnional Array to arraylist

Posted on 2012-03-23
4
Medium Priority
?
347 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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 short article about problems I had with the new location API and permissions in Marshmallow
If you’re thinking to yourself “That description sounds a lot like two people doing the work that one could accomplish,” you’re not alone.
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…
Suggested Courses
Course of the Month14 days, 18 hours left to enroll

771 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