aspose file tools*
The moose likes Java in General and the fly likes Reading Elements from an ArrayList, 3 at a time Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Reading Elements from an ArrayList, 3 at a time" Watch "Reading Elements from an ArrayList, 3 at a time" New topic
Author

Reading Elements from an ArrayList, 3 at a time

Jerri Loh
Ranch Hand

Joined: Jul 06, 2010
Posts: 31
Hi there,

at present, my code is as follows, and I know its only reading in the first 3 elements from the arrayList but how should i phrase the condition such that it reads 3 Elements from the arrayList continuously. Thanks much!

Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39409
    
  28
You do realise your for loop counts 4 elements, not 3?

Do you simply want to print 3 elements in a line? It is probably easiest to write three calls to iterator.next() and forget the inner loop. Remember you need to check with the hasNext method before trying to get a next element, otherwise you will suffer an Exception.
Jerri Loh
Ranch Hand

Joined: Jul 06, 2010
Posts: 31
1)Whoops. My output has been errrant. 4 it is. I'll note.

2) How do I "write three calls to interator.next()"?

3)Forget the inner loop? so i guess its like that...



Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39409
    
  28
Jerri Loh wrote: . . . 2) How do I "write three calls to interator.next()"? . . .
iterator.next(); iterator.next(); iterator.next();
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39409
    
  28
You will of course have to call the hasNext() method before calling next(). Otherwise you may suffer an Exception.
Jerri Loh
Ranch Hand

Joined: Jul 06, 2010
Posts: 31
is this coded correctly?

Shanky Sohar
Ranch Hand

Joined: Mar 17, 2010
Posts: 1051

Jerri Loh wrote:is this coded correctly?


No...Try this.




or.




SCJP6.0,My blog Ranchers from Delhi
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11417
    
  16

Jerri Loh wrote:is this coded correctly?


Does it do what you want?


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Shanky Sohar
Ranch Hand

Joined: Mar 17, 2010
Posts: 1051

You will of course have to call the hasNext() method before calling next(). Otherwise you may suffer an Exception.


you will suffer from exception.because
suppose you are having a array of 10 elements.then after reaching 10th element compiler simply try to fetch the 11 th value.
but if we are doing hasNext() the compiler will check whether there is any 11th value in the array or not.if yes then hasNext will return true or if not then hasnext will return false.
Jerri Loh
Ranch Hand

Joined: Jul 06, 2010
Posts: 31
ummm... ok.. let's put it this way.. i know... that

works..

my question is.. how do i call three elements out in a String continously?

this is my full code.


Shanky Sohar
Ranch Hand

Joined: Mar 17, 2010
Posts: 1051


my question is.. how do i call three elements out in a String continously?



What.not able to understand?
Jerri Loh
Ranch Hand

Joined: Jul 06, 2010
Posts: 31
hey shanky,

1)See, my programme stores all the coordinates from a text file into an arrayList. And from there I want to read out 3 elements at a time. Each element being a pair of (x, y) values already in a arrayList[...., 34.123 2.121,....].

Now I need to read every 3 elements out from the arrayList as a set of 3 together, String.

Sorry if I am confusing you. I am trying very hard to explain it.

2) Replying to your earlier reply.. you said it will cause an exception error. Do i need to throw an Element exception then?

Thank you for your guidance.





Shanky Sohar
Ranch Hand

Joined: Mar 17, 2010
Posts: 1051

Jerri Loh wrote:hey shanky,

1)See, my programme stores all the coordinates from a text file into an arrayList. And from there I want to read out 3 elements at a time. Each element being a pair of (x, y) values already in a arrayList[...., 34.123 2.121,....].

Now I need to read every 3 elements out from the arrayList as a set of 3 together, String.

Sorry if I am confusing you. I am trying very hard to explain it.

2) Replying to your earlier reply.. you said it will cause an exception error. Do i need to throw an Element exception then?

Thank you for your guidance.


i tried doing this.lets see the below programs.
Shanky Sohar
Ranch Hand

Joined: Mar 17, 2010
Posts: 1051

Here is your great programs......have a look
Shanky Sohar
Ranch Hand

Joined: Mar 17, 2010
Posts: 1051




above is possible only because of generics otherwise you cannot do like this below code

above code will give you copile time error like object cannot be added.

Then you have to do the explicit casting like this..
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39409
    
  28
shanky sohar wrote: . . .
No, that won't work, for a reason you have explained yourself earlier.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39409
    
  28
shanky sohar wrote: . . .above is possible only because of generics otherwise you cannot do like this below code

above code will give you copile time error like object cannot be added.

Then you have to do the explicit casting like this..
. . .
No, you don't need explicit casting. You can start with the empty String and add to that with the + operator.
Wouter Oet
Saloon Keeper

Joined: Oct 25, 2008
Posts: 2700

Or you can create a class Coordinate and when reading from the file create them and store them in the ArrayList.
Something like:


"Any fool can write code that a computer can understand. Good programmers write code that humans can understand." --- Martin Fowler
Please correct my English.
Shanky Sohar
Ranch Hand

Joined: Mar 17, 2010
Posts: 1051

Campbell Ritchie wrote:
shanky sohar wrote: . . .
No, that won't work, for a reason you have explained yourself earlier.



Shanky Sohar
Ranch Hand

Joined: Mar 17, 2010
Posts: 1051

This time i will not make any mistake.
see the below sample code it must work for you..


Shanky Sohar
Ranch Hand

Joined: Mar 17, 2010
Posts: 1051

I think this should be fine..it will not throw any exception.

Shanky Sohar
Ranch Hand

Joined: Mar 17, 2010
Posts: 1051

Campbell Ritchie wrote:
shanky sohar wrote: . . .above is possible only because of generics otherwise you cannot do like this below code

above code will give you copile time error like object cannot be added.

Then you have to do the explicit casting like this..
. . .
No, you don't need explicit casting. You can start with the empty String and add to that with the + operator.


here i was thinking i am correct because if the arrayList contain mixture of elements like char.number,string etc.
then it will throw an ClasscastException
but you made me correct,because iterator contains "hasnext".
which determines whether there is any element in a list irrespective of whether it is char,int,string.and also if we try to do explicit casting in that case
then it will throw classCast exception because int cannot be cast to String
or char cannot be cast to String

fo example below code give classCastException saying char cannot be cast to a String





Happy Learning
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39409
    
  28
You are correct about the repeated hasNext() calls. Those should obviate the risk of an Exception.

There is no need for any casting to Strings, is there? What is the behaviour of the + operator following a String? What happens to the object or primitive following?

And there is a better way to catenate Strings than use the += operator, isn't there?
Shanky Sohar
Ranch Hand

Joined: Mar 17, 2010
Posts: 1051

Campbell Ritchie wrote:
There is no need for any casting to Strings, is there?

Sir,
it is just shown to show that it will throw class cast exception.
Shanky Sohar
Ranch Hand

Joined: Mar 17, 2010
Posts: 1051


Campbell Ritchie wrote:

What is the behaviour of the + operator following a String? What happens to the object or primitive following?

And there is a better way to catenate Strings than use the += operator, isn't there?



yes it would be better if we use concatenate string because the plus sign is used to perform arithmetic addition.
It can also be used to concatenate strings but when we use it with a string, the operand on the right is automatically converted to a character string before being concatenated with the operand on the left.

Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19720
    
  20

shanky sohar wrote:
Campbell Ritchie wrote:

What is the behaviour of the + operator following a String? What happens to the object or primitive following?

And there is a better way to catenate Strings than use the += operator, isn't there?



yes it would be better if we use concatenate string because the plus sign is used to perform arithmetic addition.
It can also be used to concatenate strings but when we use it with a string, the operand on the right is automatically converted to a character string before being concatenated with the operand on the left.


That's not what Campbell means. After all, + is overloaded to concatenate Strings when at least one of its operands is a String.

What Campbell means is that using += is inefficient if used too many times. That's because currently is compiled into That means that for each += operation a new StringBuilder is created. It is much more efficient to use a StringBuilder explicitly. So instead of it's better to write Even better is apply a size in the StringBuilder constructor if you have an idea how large the StringBuilder will become, but in most cases you don't.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39409
    
  28
Agree; once you have a String on the left of + or +=, anything to the right is converted to a String with its toString method or the valueOf method of the String class (or both!).
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39409
    
  28
More about String catenation in the Java™ Language Specification.
Vinoth Kumar Kannan
Ranch Hand

Joined: Aug 19, 2009
Posts: 276

Rob Prime wrote:
What Campbell means is that using += is inefficient if used too many times. That's because currently is compiled into That means that for each += operation a new StringBuilder is created. It is much more efficient to use a StringBuilder explicitly.

I actually, till this very moment was thinking that 'always use assignment operators of +=,-=,.. sort whenever possible, as they are designed to be more efficient internally'. Never have thought it this way - StringBuffer object everytime. Thankssss Rob Will have that in mind...


OCPJP 6
Vinoth Kumar Kannan
Ranch Hand

Joined: Aug 19, 2009
Posts: 276

Doesn't Java create StringBuffers internally to construct Strings at run-time when we use '+' for concatenating Strings? Or StringBuilder as you say? Some sites say this and some others say that..
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39409
    
  28
StringBuilder, please. Not StringBuffer. Read their documentation, which should appear if you click the class names, for the explanation.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39409
    
  28
If you have several + signs in a single statement, leave that unchanged.That is because the compiler can optimise that, and uses StringBuilder. Don't change it, in case a better optimisation becomes available in future.

If you have several statements with + or += in, then use a StringBuilder.

And it would probably be more elegant to use a loop instead of the three next() calls, but I kept quiet about that because there appeared to be confusion about loops at the beginning of the thread. You would still require a hasNext() call somewhere.The count variable will have to be equal to 3.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39409
    
  28
Vinoth Kumar Kannan wrote:Some sites say this and some others say that..
Before Java 5, it was StringBuffer. StringBuilder was only introduced in Java 5 (spelt 1.5). You can tell that from the documentation by looking for "since". Presumably some websites have not been updated.
Shanky Sohar
Ranch Hand

Joined: Mar 17, 2010
Posts: 1051

i have learned many thing today about String.
this was a very great discussion.

Thanks.Campbell Ritchie and Rob Prime


Thanks you very much.
Vinoth Kumar Kannan
Ranch Hand

Joined: Aug 19, 2009
Posts: 276

shanky sohar wrote:i have learned many thing today about String.

So did I!
Thanks to Campbell and Rob.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39409
    
  28
You're welcome
Eva Coppola
Greenhorn

Joined: Jul 28, 2010
Posts: 10
Campbell Ritchie wrote:You're welcome


Apologies in advance for being late to this discussion - I am new to Java, studying for the SCJP... I was just wondering - would a solution to this be to use an arraylist of three elements within the loop? Is it possible to use an ArrayList within an ArrayList ? Or is that just too complicated and risking unpredictable results?
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39409
    
  28
Of course you can have Lists of Lists. You cannot specify the size of a List in advance, however, so your 3-member List can easily change to 4 members or 2 members while you're not watching.

But that is a different question from the original question. That was "how can you get 3 things out of a List?" Not, "why would anybody want things out of a List three at a time, rather than creating a class to incorporate those 3 objects as its fields?"
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Reading Elements from an ArrayList, 3 at a time