Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > looping through a list, starting at 1

Reply
Thread Tools

looping through a list, starting at 1

 
 
Chris Riesbeck
Guest
Posts: n/a
 
      08-04-2011
On 8/4/2011 3:37 AM, RedGrittyBrick wrote:
> On 04/08/2011 04:36, John B. Matthews wrote:
>> In article<(E-Mail Removed) >,
>> Roedy Green<(E-Mail Removed)> wrote:
>>
>>> I think you mean i++ not ++i.

>>
>>
>> IIUC, the result of evaluating any ForUpdate expression is discarded,
>> so I'm not sure how the difference would matter.

>
> Since I see postincrement a lot more than I see preincrement and since
> i++ is idiomatic in that construct, when seeing ++i I tend to stop and
> wonder whether the writer intended something else. So I suppose it might
> matter if engendering a sense of distrust or unease in maintenance
> programmers matters.
>


Makes no difference in this context for C and Java. In C++, an
overloaded postfix ++ for a user data structure typically has to create
and return a temporary, so modern C++ guides often recommend getting
into the ++i habit, and using i++ only when you really need it. But no
recommendation to rename the language to ++C yet.


 
Reply With Quote
 
 
 
 
Stefan Ram
Guest
Posts: n/a
 
      08-04-2011
Chris Riesbeck <(E-Mail Removed)> writes:
>But no recommendation to rename the language to ++C yet.


»Around 1983 Rick Mascitti suggested C++. It's a pun
off of the ++ operator in C, which deals with
incrementing something (although it is semantically
problematic since it should really be ++C)«

http://www.comeaucomputing.com/techtalk/

»The name C++ and its runner up ++C are fertile sources
for jokes and puns - almost all of which were known and
appreciated before the name was chosen. The name C++ was
suggested by Rick Mascitti. It was first used in
December of 1983.«

http://www2.research.att.com/~bs/bs_faq.html

 
Reply With Quote
 
 
 
 
markspace
Guest
Posts: n/a
 
      08-04-2011
On 8/4/2011 10:47 AM, Chris Riesbeck wrote:
> In C++, an
> overloaded postfix ++
>



See? This is why I'm against any kind of operator overloading in Java.
Too much stupid out there.


 
Reply With Quote
 
markspace
Guest
Posts: n/a
 
      08-04-2011
On 8/4/2011 1:58 PM, Patricia Shanahan wrote:
> On 8/4/2011 11:27 AM, markspace wrote:
>> On 8/4/2011 10:47 AM, Chris Riesbeck wrote:
>>> In C++, an
>>> overloaded postfix ++
>>>

>> See? This is why I'm against any kind of operator overloading in Java.
>> Too much stupid out there.

>
> So what operator should have been used for String concatenation, instead
> of overloading "+"?



I think the obvious interpretation of my comment should be "user defined
operator overloading."


 
Reply With Quote
 
Eric Sosman
Guest
Posts: n/a
 
      08-05-2011
On 8/4/2011 4:58 PM, Patricia Shanahan wrote:
> On 8/4/2011 11:27 AM, markspace wrote:
>> On 8/4/2011 10:47 AM, Chris Riesbeck wrote:
>>> In C++, an
>>> overloaded postfix ++
>>>

>> See? This is why I'm against any kind of operator overloading in Java.
>> Too much stupid out there.

>
> So what operator should have been used for String concatenation, instead
> of overloading "+"?


(Aside: I know of a language that used "||" for this purpose.)

This "overload" is feeble at best: "+" is overloaded six ways,
"-" five, "*", "/", "%", "++", "--", "<", "<=", ">", and ">=" four
each. "==" and "!=" and "=" and "." and "instanceof" are overloaded
to an infinite degree (countable, I think). So what?

--
Eric Sosman
http://www.velocityreviews.com/forums/(E-Mail Removed)d
 
Reply With Quote
 
Stefan Ram
Guest
Posts: n/a
 
      08-05-2011
Eric Sosman <(E-Mail Removed)> writes:
>This "overload" is feeble at best: "+" is overloaded six ways,
>"-" five, "*", "/", "%", "++", "--", "<", "<=", ">", and ">=" four
>each. "==" and "!=" and "=" and "." and "instanceof" are overloaded
>to an infinite degree (countable, I think). So what?


The JLS 3 defines »overloading« for methods in 8.4.9 and 9.4.2
and for constructors in 8.8.8. So one cannot derive from the
JLS that any operator in Java is overloaded at all.

One also might define it semantically as »having several
different meanings (depending on the static argument type)
when expressed in the English language«.

Then, »+« has two overloads »plus« and »concatenated with«,
but one might get by with counting only one overload »plus«
when one can mentally subsume string concatenation under the
English conjunction of »plus«.

»+« for strings has something natural, given then it makes
»length« to be similar to a kind of

http://en.wikipedia.org/wiki/Ring_homomorphism

(not precisly), given that

length( s + s1 )= length( s )+ length( s1 ),
length( s + "" )= length( s )+ 0,

and, with Perl's »x« operator (written as »*« below), for
an int value i, even

length( s + s + ... + s [i times] )= length( s * i )
length( s + s1 * i )= length( s )+ length( s1 )* i,
length(( s + s1 )* i )=( length( s )+ length( s1 ))* i
length( "" * i )= 0 * i, and possibly more

hold.

 
Reply With Quote
 
Raymond Tong
Guest
Posts: n/a
 
      08-05-2011
If the List has implemented RandomAccess interface, the latter
approach (i.e. calling get(i)) is preferred.
Otherwise, the former (i.e. getting the iterator) is preferred.

e.g. ArrayList implemented RandomAccess which backed by Array.
Calling get(i) simply return the array element. But has little
overhead on creating the Iterator instance.

e.g. LinkedList does NOT implement RandomAccess
Calling get(i) needed to iterate from the first node until (i)th
element.
But create the Iterator once would same time for a large size list.

In conclusion, depeneds on the implementation of your list.
if (list instanceof RandomAccess) {
// latter
} else {
// former
}
 
Reply With Quote
 
RedGrittyBrick
Guest
Posts: n/a
 
      08-05-2011
On 04/08/2011 22:46, Patricia Shanahan wrote:
> On 8/4/2011 2:36 PM, markspace wrote:
>> On 8/4/2011 1:58 PM, Patricia Shanahan wrote:
>>> On 8/4/2011 11:27 AM, markspace wrote:
>>>> On 8/4/2011 10:47 AM, Chris Riesbeck wrote:
>>>>> In C++, an
>>>>> overloaded postfix ++
>>>>>
>>>> See? This is why I'm against any kind of operator overloading in Java.
>>>> Too much stupid out there.
>>>
>>> So what operator should have been used for String concatenation, instead
>>> of overloading "+"?


We could use '.'

Then we could use '->' for method invocation.

And '::' for class namespace separators (or whatever they are called).



>> I think the obvious interpretation of my comment should be "user defined
>> operator overloading."
>>
>>

>
> Why should the appropriateness of an operator overloading be affected by
> who did it?


It shouldn't but I imagine markspace's objection might have been that
whilst we all learn about the varied uses of '+' at the start of
learning Java, I expect it can be a little perplexing when you encounter
a '+' that makes no sense until you find where the original programmer
defined an overload of that operator. Even worse might be a '+' that
apparently makes sense but where you do not realise that the operator
has been overloaded. Is this possible?

--
RGB
 
Reply With Quote
 
Raymond Tong
Guest
Posts: n/a
 
      08-05-2011
It depends on the implmentation of list.
If the list implements RandomAccess interface, it is recommended to
call get(i) instead of using iterator().

e.g. ArrayList implements RandomAccess interface and backed by Array.
Calling get(i) would simply return (i)th element in an array which is
quick.
Creating an iterator has little overhead on creating an instance of
Iterator object.

e.g. LinkedList does NOT implement RandomAccess.
Calling get(i) need to look from the fist node to (i)th element which
is slow if i is large.
Creating an iterator is one time action which save time for sequential
lookup.

if (list instanceof RandomAccess) {
// get(i)
} else {
// iterator
}
 
Reply With Quote
 
Andreas Leitgeb
Guest
Posts: n/a
 
      08-05-2011
markspace <-@> wrote:
> On 8/4/2011 10:47 AM, Chris Riesbeck wrote:
>> In C++, an overloaded postfix ++ ...

> See? This is why I'm against any kind of operator overloading in Java.


*All* generalizations are bad.

This little wart of idiomatic postIncr extended from primitives
(where it didn't really matter) to object types (where it does
matter) shouldn't be generalized to all operators.

(and the other "wart" about overloading << and >> for streams
in C++ is just subjective judgement. I don't find it all that
bad, myself. It fits well to the look of the symbol indicating
direction of data-flow, unlike e.g. mathematical ops, which
are "just" convention. )

> Too much stupid out there.


Maybe Java just isn't for the masses -- they all have their
own mind on coding, not adhering to my own. (*irony*)

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
looping through json array loops through the characters instead ofthe values Aaron Javascript 2 04-10-2011 05:58 PM
looping through array starting at i !=0 John Smith Ruby 2 10-07-2010 10:58 AM
3 ESSENTIAL TOOLS FOR STARTING AND MAINTAINING...3 ESSENTIAL TOOLSFOR STARTING AND MAINTAINING...3 ESSENTIAL TOOLS FOR STARTING ANDMAINTAINING... Oanh Bui C++ 0 04-27-2009 12:51 PM
3 ESSENTIAL TOOLS FOR STARTING AND MAINTAINING...3 ESSENTIAL TOOLSFOR STARTING AND MAINTAINING...3 ESSENTIAL TOOLS FOR STARTING ANDMAINTAINING... Oanh Bui C Programming 0 04-27-2009 12:51 PM
3 ESSENTIAL TOOLS FOR STARTING AND MAINTAINING...3 ESSENTIAL TOOLSFOR STARTING AND MAINTAINING...3 ESSENTIAL TOOLS FOR STARTING ANDMAINTAINING... Oanh Bui Python 0 04-27-2009 12:46 PM



Advertisments