• 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
  • Tim Cooke
  • Devaka Cooray
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Martijn Verburg
  • Frits Walraven
  • Himai Minh

Operation on List

 
Greenhorn
Posts: 6
Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi
Can someone please provide me an example for below scenario:

mainList
list1
list2

list1 contains : 1 2 3 4 5
list2 contains : A B C D E

mainList should contain : 1-A 2-B 3-C 4-D 5-E

Thanks
 
Bartender
Posts: 825
5
Python Ruby Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Having two lists with that content, what you need to do is:

take the first value from list1 and add it to mainList
take the first value from list2 and add it to mainList
take the second value from list1 and add it to mainList
take the second value from list2 and add it to mainList
...
take the n-th value from list1 and add it to mainList
take the n-th value from list2 and add it to mainList

Not so hard, right? But nobody's gonna implement that for you, you need to do it yourself.
By the way, I supposed those dashes between should not be part of mainList.
 
Ranch Hand
Posts: 277
Oracle Spring Flex
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Post us what you have done so far. We will guide you.

 
Tahir Ahm
Greenhorn
Posts: 6
Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi All

Thanks for the reply!! Much appreciated :-)

I'm trying to implement a dropdown, which displays a list as " id-name "
dash - is part of mainList....


List<String> nameList = new ArrayList<String>(0);
List<String> idList = new ArrayList<String>(0);
List<String> mainList = new ArrayList<String>(0);
int i=0;

for (Alignment entry : studentList) { //Alignment is table in DB
nameList.add(entry.getName());
idList.add(entry.getId());

/*Code for mainList goes here

mainList.addALL(i,nameList);
mainList.add(i,"-");
mainList.addALL(i,nameList);
i++;
*/
}



Its entering infinite loop, i guess... Taking longer time to load the page.
 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Tahir Ahm wrote:Its entering infinite loop, i guess... Taking longer time to load the page.


Frankly, I'm surprised it does anything at all, because what you've written won't compile. When posting code, please make sure it's exact (use copy/paste if you need to; I'd also suggest that you read this page).

However, for your needs, have you considered a Map (java.util.Map) instead?

Winston

 
Kemal Sokolovic
Bartender
Posts: 825
5
Python Ruby Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well, as Mr Winston said, that code does nothing. Even if you meant to post some kind of pseudo code or whatever, it's still wrong.

I would follow his advice and take a look at how Map works. Using it, you'll avoid having to keep data in two separate lists, but you'll have everything you need in one place.

I thought you are just practicing programming, so I proposed perhaps not so good solution for your issue (didn't have more details from your first post).
 
Tahir Ahm
Greenhorn
Posts: 6
Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello All

Thanks for the Feedback!! The code that was pasted was just part of main program, I know the code does nothing and thats the reason I was here.

Anyways I managed to get desired result, pasting the little snippet here...

For (.....) //loop
{
mainList.add(entry.getId()+" - "+entry.getName());
}


Thanks
Greenhorn
 
Message for you sir! I think it is a tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic