• Post Reply Bookmark Topic Watch Topic
  • New Topic

Java Repeat Statement  RSS feed

 
Wendy Jackson
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
here I have an abstract class and two concrete classes called Men and Women. my main method is implemented with and array List

I have a method which I basically want to simulate first filling the queue and then after that removing the first person and adding a new person each time in my public void mySimulation() method



any suggestions on how I could repeat this process such that I will first check that the queue is full,

if so remove the first item and add a new person to it using my arrayList each time as I cant get my head round this at the moment?

My public void mySimulation() method works but does not give me the required output.
 
Junilu Lacar
Sheriff
Posts: 11477
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Wendy Jackson wrote:My public void mySimulation() method works but does not give me the required output.

That's kind of like saying, "My car works but I can't get it to go where I want it to go."  If it doesn't give you the required output, how can you say it works? Isn't that actually the definition of "NOT working"?

Also, "thePeople" as a type name does not follow Java naming conventions. Class and interface names should be capitalized, so that should be "ThePeople" instead. As far as the name itself, "ThePeople" doesn't really make any sense. Can't you find a better name? That's like calling the superclass for Dog, Cat, & Fish classes "ThePets". A better name would be just "Pet".

As for a repeat statement, Java has several loops.

Search for:
Java for loop
Java while and do-while loop
 
Wendy Jackson
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks for the suggestion, i will make these changes. i have thought of this using a for loop


but this does not throw control back to the remove part of my code.
 
Knute Snortum
Sheriff
Posts: 4274
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't understand the question.  You obviously know how to write a loop, so what's the problem writing a loop around the whole "remove" code.
 
Wendy Jackson
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks for the reply I found out by altering my for statement and including my if into this for statement.
Strangely enough when I use my enque and deque methods shown below this works but not as expected as an arraylist will work normally.

If I use list.remove(0) and list.add() this works perfectly meaning there are errors in my enque and deque methods below:






my question is how different is an arraylist different from an array in terms of deque and enque. judging from the fact that i am employing a circular arrayList even though I have wrapped around do I have to shift left after dequeing and equeing?
 
Wendy Jackson
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ok so i found out using an arraylist for a circular queue is "broken". and this means one  can use an ArrayDeque

http://grepcode.com/file/repository.grepcode.com/java/root/jdk/openjdk/6-b27/java/util/ArrayDeque.java#ArrayDeque.addLast%28java.lang.Object%29

could someone guide me on how this can be achieved for my deque and enque method?
 
Campbell Ritchie
Marshal
Posts: 56529
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Who said that ArrayList is broken? I think what they mean is that ArrayList is not designed for use as a circular queue. You probably can use an ArrayList as a queue if you insist:-Now, isn't that dreadful code!
Using a Deque is quite easy, as you will find out from the Java™ Tutorials (see also next section):-Note the similarity between the two versions, and also that the methods called on the Queue may throw Exceptions if something goes wrong. See the tutorials link for more details, and the documentation for the different types. ArrayDeque is a Deque as well, which means you can run the queue forwards or backwards. Remember Deque is pronounced deck.
 
Wendy Jackson
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the explanation, with this knowledge I should be able to build on now.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!