Solved

two dimesnional Array to arraylist

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

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

Does the idea of dealing with bits scare or confuse you? Does it seem like a waste of time in an age where we all have terabytes of storage? If so, you're missing out on one of the core tools in every professional programmer's toolbox. Learn how to …
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…

707 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now