• 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 arrenge problem in Core Java

 
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have two arrays .

int array1 [] = {1,2,3,4,5,6,7};

int array2 [] = {1,2,3};

array1 should be parent loop . I have to arrange like below :

Expected result :
1-1
2-2
3-3
4-1
5-2
6-3
7-1


How can I proceed please suggest .
 
Bartender
Posts: 1952
7
Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please walk us through what you've come up with so far, and where you're stuck exactly.
 
Ranch Hand
Posts: 529
19
Eclipse IDE MySQL Database Tomcat Server Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
@ Santanu Guha Is there any chance 2nd loop will have more elements than 1st ?
 
Santanu Guha
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
First loop will have always more elements than second loop
But both loop any number of elements. I need code .
 
Rancher
Posts: 4801
50
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If you look at the expected output then I would say that's a single loop, with an additional counter to indicate which element in the second array to use.
 
Marshal
Posts: 79153
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
We do not condone complete solutions if the OP has not had a chance to post.
 
Santanu Guha
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
@Dave Tolls & Ganish Patil : can you give code snippet so that I can understand better ......

because the first array can grow to any number of elements .
second array elements also grow to any number up to count 30 .

But first array count will be huge but second array count limit is 30 max .

 
Ganish Patil
Ranch Hand
Posts: 529
19
Eclipse IDE MySQL Database Tomcat Server Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Where is the code which I posted here ?
 
lowercase baba
Posts: 13089
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Ganish Patil wrote:Where is the code which I posted here ?


It was removed by the moderators. Please see Campbell's post four above this one.
 
Ganish Patil
Ranch Hand
Posts: 529
19
Eclipse IDE MySQL Database Tomcat Server Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I confess for that really didn't know before as I'm also new to this community.
@Guha you can do that in a loop of first array which has max elements & just use a counter as an index of second array. Put condition in that loop if counter reaches max index of second array then make it 0 again to start from 0th index's element of array two. But rather than asking you should at least try else no one is going to give you whole code and explain whole code how to write. Try out your self else this is not going to work like spoon feeding. No offense ! enjoy coding
 
Campbell Ritchie
Marshal
Posts: 79153
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
No, that is not at all a good way to do it.
 
Campbell Ritchie
Marshal
Posts: 79153
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Ganish Patil wrote: . . . you should at least try . . .

Agree

If somebody tries and fails we shall do our best to help them out. If they then work it out they will have learnt something useful. If somebody simply fives them the code they will learn nothing.
 
Ganish Patil
Ranch Hand
Posts: 529
19
Eclipse IDE MySQL Database Tomcat Server Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
@ Campbell Absolutely..
 
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Ganish Patil wrote:
Put condition in that loop if counter reaches max index of second array then make it 0 again to start from 0th index's element of array two.



Or how about using % operator and avoid reset
 
Campbell Ritchie
Marshal
Posts: 79153
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think OP has had long enough to work out the solution, which is actually quite simple.
Agree with Karthick Pattabiraman about using the remainder operator. A much better way to do it.

I think it is now possible to restore the code which Ganish Patil posted on Wednesday, and it probably can't do any harm now.Apart from the stylistic problems (I removed several unnecessary blank lines), the logic could do with a lot of improvement.
 
Karthick Pattabiraman
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ok then, here is my solution



and if at all I need a reset approach I would go with Exception handling. That's more understandable and elegant in my opinion.

 
Bartender
Posts: 2236
63
IntelliJ IDE Firefox Browser Spring Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Karthick Pattabiraman wrote:That's more understandable and elegant in my opinion.


I strongly disagree. Exception handling should not be used for normal control flow.

I would write
 
Campbell Ritchie
Marshal
Posts: 79153
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Karthick Pattabiraman wrote:Ok then, here is my solution

Ouch! A for‑each loop and incrementing counters? No.

and if at all I need a reset approach I would go with Exception handling. . . .

Afraid that is even worse.
 
Campbell Ritchie
Marshal
Posts: 79153
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Paweł and I would seem to be thinking the same way. His solution and mine would have been the same. Note it has one line less code, too.
 
Ganish Patil
Ranch Hand
Posts: 529
19
Eclipse IDE MySQL Database Tomcat Server Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I didn't really think of this b[i % b.length]) , good one !
 
Campbell Ritchie
Marshal
Posts: 79153
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you.
 
I wasn't selected to go to mars. This tiny ad got in ahead of me:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic