This week's book giveaway is in the Other Languages forum.
We're giving away four copies of Rust Web Development and have Bastian Gruber on-line!
See this thread for details.
Win a copy of Rust Web Development this week in the Other Languages forum!
  • 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:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Junilu Lacar
  • Rob Spoor
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Jesse Silverman
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Frits Walraven

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: 74627
335
  • 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: 13018
66
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: 74627
335
  • 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: 74627
335
  • 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: 74627
335
  • 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: 74627
335
  • 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: 74627
335
  • 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: 74627
335
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you.
 
You showed up just in time for the waffles! And this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic