aspose file tools*
The moose likes Java in General and the fly likes Operation on List Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Operation on List" Watch "Operation on List" New topic
Author

Operation on List

Tahir Ahm
Greenhorn

Joined: May 17, 2012
Posts: 6

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

Joined: Jun 19, 2010
Posts: 825
    
    5

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.


The quieter you are, the more you are able to hear.
Ashwin Sridhar
Ranch Hand

Joined: Jul 09, 2011
Posts: 272

Post us what you have done so far. We will guide you.


Ashwin Sridhar
SCJP | SCWCD | OCA
Tahir Ahm
Greenhorn

Joined: May 17, 2012
Posts: 6

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

Joined: Mar 17, 2011
Posts: 7807
    
  21

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


Isn't it funny how there's always time and money enough to do it WRONG?
Articles by Winston can be found here
Kemal Sokolovic
Bartender

Joined: Jun 19, 2010
Posts: 825
    
    5

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

Joined: May 17, 2012
Posts: 6

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
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Operation on List