Solved

two dimesnional Array to arraylist

Posted on 2012-03-23
4
345 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 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

Is Your Team Achieving Their Full Potential?

74% of employees feel they are not achieving their full potential. With Linux Academy, not only will you strengthen your team's core competencies but also their knowledge of of the newest IT topics.

With new material every week, we'll make sure that you stay ahead of the game.

Question has a verified solution.

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

Whether you've completed a degree in computer sciences or you're a self-taught programmer, writing your first lines of code in the real world is always a challenge. Here are some of the most common pitfalls for new programmers.
A short article about problems I had with the new location API and permissions in Marshmallow
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…
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

690 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