Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7187669
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T18:53:55+00:00 2026-05-28T18:53:55+00:00

I have an integer array of size 50. So, as you know, initially the

  • 0

I have an integer array of size 50.
So, as you know, initially the element values of this array will be all 0’s.
What I need to do is:
If the array = [12,10,0,0,……0], the for loop needs to consider only the non-zero elements, so the loop should get executed only twice..once for 12 and next for 10.
Similarly, if the array is = [0,0,0,……0], then for loop should not executed at all since all elements are zero.
If the array is [12,10,5,0,17,8,0,0,…….,0] then the for loop should get executed for the first 6 elements, even though one of the inner elements is zero.

Here is what I have.This gives me an IndexOutOfBoundsException.Please help.
Also, is there any way I can dynamically increase the size of an int array rather than setting the size to 50,and then use it in the loop? Thx in advacance for any help!

int[] myArray = new int[50];
int cntr = 0;
int x = getXvalue();
int y = getYvalue();

if (x>y){
   myArray[cntr] = x;
   cntr++;
     }

 for (int p=0; p<= myArray.length && myArray[p]!=0 ; p++){
//execute other methods..
}

//SOLUTION:

I’ve made use of an ArrayList instead of an int array to dynamically increase size of the array.

ArrayList<Integer> myArray = new ArrayList<Integer>();

To set the value of the elements-
myArray.add(cntr, x); // add(index location, value)
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T18:53:56+00:00Added an answer on May 28, 2026 at 6:53 pm

    A cleaner way is:

    The most convenient way to solve such things like this is using the appropriate Collection. For this example you might consider using an ArrayList<Integer>.

    The advantage is that you do not have to preinitialize the list with a given size. It will also resize if necessary.

    Another advantage is that you do not have to care about padding empty elements as well as handling empty elements at the end of the list.

    The concrete problem is:

    for (int p=0; p<= myArray[cntr].length && myArray[p]!=0 ; p++){
    //execute other methods..
    }
    

    This should be illegal:

    myArray[cntr].length
    

    it needs to be myArray.length

    also you iterate from 0..50 with <= this adresses 51 elements you do only have 50 elements so the correct line of code would be

    for (int p=0; p< myArray.length && myArray[p]!=0 ; p++){
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a pointer to integer array of 10. What should dereferencing this pointer
I have an integer array in my Objective-C program. I'd like to sort it
I have a array of integer numbers (say 1,2,3,4,5 or 1,2,3, ... 10 or
I have an array of integers like these; dim x as integer()={10,9,4,7,6,8,3}. Now I
I have an array with integers of values from 0 to 100. I wish
I have a program that has to declare a huge integer array of the
this is really strange, i have an array in delphi and fill it with
This question was inspired by a similar question: How does delete[] know the size
I have an undetermined size for a dataset based on unique integer keys. I
I have an array in my PHP application, and I need to develop a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.