Java Forum / General / December 2005
Asking inverse() function for List and ArrayList
moop - 12 Dec 2005 07:53 GMT Hi, I think to have a function to inverse a list is quite natural to be embedded into the standard JSE library, how do you think about that?
IchBin - 12 Dec 2005 08:05 GMT moop™ wrote:
> Hi, > I think to have a function to inverse a list is quite natural to be > embedded into the standard JSE library, how do you think about that? If I understand you question.. look at this link..
http://javaalmanac.com/egs/java.util/coll_SortList.html
 Signature Thanks in Advance... IchBin, Pocono Lake, Pa, USA http://weconsultants.servebeer.com/JHackerAppManager __________________________________________________________________________
'If there is one, Knowledge is the "Fountain of Youth"' -William E. Taylor, Regular Guy (1952-)
Roedy Green - 12 Dec 2005 09:09 GMT >inverse a list what do you mean my that, sort a list in reverse order? make the first last and the last first etc. Both of those are quite easy to do.
 Signature Canadian Mind Products, Roedy Green. http://mindprod.com Java custom programming, consulting and coaching.
abigale_carson@yahoo.com - 12 Dec 2005 09:34 GMT See http://java.sun.com/j2se/1.4.2/docs/api/java/util/Collections.html#reverse(java. util.List) (or if it breaks, http://linkfrog.net/dabt).
Abigale
Rhino - 12 Dec 2005 14:11 GMT > Hi, > I think to have a function to inverse a list is quite natural to be > embedded into the standard JSE library, how do you think about that? It depends on what you mean by inversing a list.
If you mean that a given list contains 0, 1, 2, 3, and 4 and you want the inverse function to give you the other digits, i.e. 5 through 9 inclusive, that might be reasonable, as long as you are willing to ignore that some cultures have their own number systems that don't use the base 10 digits.
You'd have similar issues if you wanted to inverse letters. If a given list contains 'A', 'B', and 'C' what inverse would you want to see? If you are an English-speaker, you'd presumably want 'D' through 'Z' inclusive. But other cultures that used other languages, alphabets, and scripts would want something quite different.
At the very least, your inverse function would need to be very culturally-sensitive if it was to work beyond the confines of English and base 10 arithmetic.
Rhino
Francesco Devittori - 12 Dec 2005 15:32 GMT >>Hi, >>I think to have a function to inverse a list is quite natural to be [quoted text clipped - 18 lines] > > Rhino Given a list {0, 1, 2, 3, 4} I suppose the OP means a function that returns {4, 3, 2, 1, 0}. Keep it simple :-)
Francesco
Rhino - 12 Dec 2005 17:18 GMT >>>Hi, >>>I think to have a function to inverse a list is quite natural to be [quoted text clipped - 22 lines] > Given a list {0, 1, 2, 3, 4} I suppose the OP means a function that > returns {4, 3, 2, 1, 0}. Keep it simple :-) You could be right but I think the key to the analysis of this problem is to understand what the original poster meant by 'inverse', not just assume that it was something simple. It's entirely possible that he/she _did_ want the inverse of {0,1,2,3,4} to be {5,6,7,8,9} or maybe something else altogether, not {4,3,2,1,0}. We can't know until we clarify what he/she meant. And until you understand the question, you can't give a good answer.
Rhino
Francesco Devittori - 12 Dec 2005 19:15 GMT >>>>Hi, >>>>I think to have a function to inverse a list is quite natural to be [quoted text clipped - 31 lines] > > Rhino You are right, but what you say is not the inverse of a list but the inverse of each object in the list, which is something different.
list.inverse() vs. for(e: list) e.inverse();
(not even sure one could inverse an int anyway)
Francesco
IchBin - 12 Dec 2005 21:28 GMT >>>>> Hi, >>>>> I think to have a function to inverse a list is quite natural to be [quoted text clipped - 41 lines] > > Francesco Shame the OP does not return a comment to the intention.
 Signature Thanks in Advance... IchBin, Pocono Lake, Pa, USA http://weconsultants.servebeer.com/JHackerAppManager __________________________________________________________________________
'If there is one, Knowledge is the "Fountain of Youth"' -William E. Taylor, Regular Guy (1952-)
Oliver Wong - 12 Dec 2005 22:53 GMT >>>>>Hi, >>>>>I think to have a function to inverse a list is quite natural to be [quoted text clipped - 41 lines] > > Francesco When someone says the "inverse of an integer", they probably mean the modular inverse. That means you need to have some sort of implied modulo m, and the inverse (b^-1) of an integer b is such that b (b^-1) = 1 (mod m).
If m is not prime, then not every integer will have an inverse.
But for what it's worth, I had assumed the OP wanted the list returned in reverse order, like Francesco initially did.
- Oliver
moop - 13 Dec 2005 12:36 GMT I am so so so sorry for my innocent of Java Collections that I dont know the existent of Collections.reverse(), exactly what IchBin replied next to my post is the answer I am looking for. I dont know my ask for help attracts your attentions so much, I am sorry for my confused question. so I am very much thanks about all of you for your kindness reactions, I feel so warm for your helps, thanks a lot!! Thanks!
andreaz - 13 Dec 2005 16:32 GMT You can use swap static method http://java.sun.com/j2se/1.4.2/docs/api/java/util/Collections.html#swap(java.uti l.List,%20int,%20int) for all elements of the list starting from the last and first until the middle of the list; or write your own method it's not difficult.
Free MagazinesGet these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...
|
|
|