• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Array looping

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am trying to print a loop inside an array of a greater size than the loop itself.
Like for example I have an array of size 7 but it has only 3 elements.

int[] array=new int[7]
array[0]=2;
array[1]=3;
array[2]=4;
now what I want to do is print these three numbers in a loop so that my array[3]=2;array[4]=3;array[5]=4 ...... till the last one. Can anyone PLEASE give me a solution to this.Also the array could be any size and not just 7.
 
Bartender
Posts: 3323
86
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Zeeshan AliNoxiee wrote:I am trying to print a loop inside an array of a greater size than the loop itself.
Like for example I have an array of size 7 but it has only 3 elements.


No, it has 7 elements but only 3 of them are values you have inserted.

To iterate over the elements you have inserted rather than the whole array you can either keep track of the number of values you have inserted or initially fill the array with an invalid value (eg -1) and iterate until you find an invalid value.

Zeeshan AliNoxiee wrote:
now what I want to do is print these three numbers in a loop so that my array[3]=2;array[4]=3;array[5]=4 ...... till the last one.


Sorry but I don't know what you mean by this. You talk of printing the values but then show what looks like a copy of the elements to other elements in the array. Please explain clearly what you are trying to achieve
 
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Just add this for loop:
for (int i = 0; i < array.length; i++;)
array[i] = array[i % 3];
I didn't test this code so it might need some tweaking, but the general idea will work.

If your code needs to work on different source inputs it would be better to add another array.
 
Tony Docherty
Bartender
Posts: 3323
86
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Mark,
Thanks for helping out but please don't provide coded solutions in the beginners forum. In this forum we try to guide the OP to write the code themselves rather than just giving them a solution.
 
Mark King
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Tony Docherty wrote:Hi Mark,
Thanks for helping out but please don't provide coded solutions in the beginners forum. In this forum we try to guide the OP to write the code themselves rather than just giving them a solution.


Sorry, I am new at posting here. Thanks for pointing it out so I that I shouldn't do it again.
 
Tony Docherty
Bartender
Posts: 3323
86
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
No problem and thanks for understanding.
reply
    Bookmark Topic Watch Topic
  • New Topic