Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Operation on List

 
Tahir Ahm
Greenhorn
Posts: 6
Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • 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
 
Kemal Sokolovic
Bartender
Posts: 825
5
Java Python Ruby
  • Mark post as helpful
  • send pies
  • 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.
 
Ashwin Sridhar
Ranch Hand
Posts: 277
Flex Oracle Spring
  • Mark post as helpful
  • send pies
  • 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
  • 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.
 
Winston Gutkowski
Bartender
Pie
Posts: 10417
63
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • 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
Java Python Ruby
  • Mark post as helpful
  • send pies
  • 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
  • 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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic