1 Replies - 377 Views - Last Post: 21 February 2010 - 03:02 AM Rate Topic: -----

#1 Java Student  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 21
  • View blog
  • Posts: 481
  • Joined: 05-February 10

Understanding ArrayLists

Posted 21 February 2010 - 01:06 AM

Ok so i this one bit of code im cufused about.
In Main class i make the ArrayList variable "b" thats referring to a seperate class called "Bullets"
ArrayList<Bullets>b = new ArrayList<Bullets>();


This is from the Update() method.
if(S==1) {
			Bullets add = p1.getBull();
			add.xVel = 3;
			b.add(add);
			S++;
		}



In the Update() method. Can you please explain in detail what the .get and .add are all about in association with the loop. Iv searched EVERYWHERE to find information but cant wrap my head around it.

for(int x = 0; x<b.size(); x++) {
			b.get(x).move();
			if(b.get(x).rect.intersects(p1.rect)&&b.get(x).xVel<0) {
				p1.health--; //Decrease players health once hit
				b.remove(x); //Once bullet hit player, remove bullet from screen
				x--;
				continue;
			}


Questions:

In " b.get(x).move(); " is the (x) referring to an element in the array or an x variable in my Bullets.move() method?

In " b.add(add); " I'd really like to know what its doing in relation with the whole if statement?

In " b.remove(x); " how does removing the (x) element from the array make the bullet disappear from the screen?

Answers would be EXTREMELY appreciated ! :bigsmile: you would esentially be a god to me if you answered all my questions

Is This A Good Question/Topic? 0
  • +

Replies To: Understanding ArrayLists

#2 g00se  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2720
  • View blog
  • Posts: 11,438
  • Joined: 20-September 08

Re: Understanding ArrayLists

Posted 21 February 2010 - 03:02 AM

The only person who can answer the questions about how this program works is the author, but as far as the questions relating to ArrayList are concerned:

Quote

In " b.get(x).move(); " is the (x) referring to an element in the array or an x variable in my Bullets.move() method?


The program gets element indexed 'x' in the List (a Bullets instance) and calls move on it

Quote

In " b.add(add); " I'd really like to know what its doing in relation with the whole if statement?


It's getting the Bullets from 'p1' (whatever that is) and adding to the end of the List 'b'
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1