8 Replies - 222 Views - Last Post: 13 August 2012 - 10:01 AM Rate Topic: -----

#1 worldofwar  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 40
  • Joined: 26-June 11

Array checker?

Posted 10 August 2012 - 04:46 AM

Hello,

What this does is it sends a charm drop to an npc, now I want to make it so certain npcs don't get that drop..

I tried it like this, I don't know how to make it check all instead of doing noCharm[1] && noCharm[2] etc.

int random = Utils.random(30);

int[] noCharms = {4278, 4279, 4280, 4282, 4283, 4284};

if (random >= 1 && random <= 5 && this.getId() != noCharms[]) {
	sendDrop(killer, new Drop(12158, 60.0, 1, 1, false));
} else if (random >= 6 && random <= 11 && this.getId() != noCharms[]) {
	sendDrop(killer, new Drop(12159, 60.0, 1, 1, false));
} else if (random >= 12 && random <= 15 && this.getId() != noCharms[]) {
	sendDrop(killer, new Drop(12160, 60.0, 1, 1, false));
} else if (random == 15 && this.getId() != noCharms[]) {
	sendDrop(killer, new Drop(12163, 60.0, 1, 1, false));
}



This obviously doesn't work yet, but I'd like to know how to make it check all the ints in the noCharm array..

Thank you.

This post has been edited by worldofwar: 10 August 2012 - 04:47 AM


Is This A Good Question/Topic? 0
  • +

Replies To: Array checker?

#2 sepp2k  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 1690
  • View blog
  • Posts: 2,553
  • Joined: 21-June 11

Re: Array checker?

Posted 10 August 2012 - 04:52 AM

You need to iterate over the noCharms array in a for-loop and compare every item to the result of getId().
Was This Post Helpful? 1
  • +
  • -

#3 Luckless  Icon User is offline

  • </luck>
  • member icon

Reputation: 291
  • View blog
  • Posts: 1,141
  • Joined: 31-August 09

Re: Array checker?

Posted 10 August 2012 - 04:56 AM

Use a HashSet--this is what they are made for. A set stores unique values and provides a .contains() method:

HashSet<int> noCharm = new HashSet<int>();
//add exclusions
noCharm.add(4278);
noCharm.add(4279);
noCharm.add(4280); 
noCharm.add(4282);
noCharm.add(4283);
noCharm.add(4284);

if (random >= 1 && random <= 5 && !noCharms.contains(this.getId())) {
...
}
...




Now for the more serious offense, why all the if statements?
Was This Post Helpful? 1
  • +
  • -

#4 worldofwar  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 40
  • Joined: 26-June 11

Re: Array checker?

Posted 10 August 2012 - 05:07 AM

View Postsepp2k, on 10 August 2012 - 04:52 AM, said:

You need to iterate over the noCharms array in a for-loop and compare every item to the result of getId().


Tried this:

	public boolean noCharm() {
		int[] noCharms = {4278, 4279, 4280, 4282, 4283, 4284};
		for(int i = 0; i < noCharms.length ; i++) {
			if(this.getId() == noCharms[i]) {
				return true;
			}
		}
		return false;
	}



and made it check for the boolean, but that didn't work out so well.

Edit: Nevermind, it did work, I had an i instead of noCharm[i]..
I feel so stupid right now.. Thank you :)

View PostLuckless, on 10 August 2012 - 04:56 AM, said:

Use a HashSet--this is what they are made for. A set stores unique values and provides a .contains() method:

HashSet<int> noCharm = new HashSet<int>();
//add exclusions
noCharm.add(4278);
noCharm.add(4279);
noCharm.add(4280); 
noCharm.add(4282);
noCharm.add(4283);
noCharm.add(4284);

if (random >= 1 && random <= 5 && !noCharms.contains(this.getId())) {
...
}
...




Now for the more serious offense, why all the if statements?


Thank you very much, works like a charm :D

But sorry for being a noob, but what could I replace all the if statements with then?

a switch?

switch(random) {
case 1:
case 2:
..
break;
case 3:
..
break;
}

This post has been edited by worldofwar: 10 August 2012 - 05:16 AM

Was This Post Helpful? 0
  • +
  • -

#5 Luckless  Icon User is offline

  • </luck>
  • member icon

Reputation: 291
  • View blog
  • Posts: 1,141
  • Joined: 31-August 09

Re: Array checker?

Posted 10 August 2012 - 05:16 AM

I just wanted to know the structure of this class. I think there is probably a more efficient design. The rule of thumb for me and others is if you have more than three, there is probably a better way, but that's ok. I'm glad that worked for you. Google HashSet, I know there are more lovely ways to add arrays of things to a HashSet in one fell swoop, I just don't remember the exact syntax and I'm at work
Was This Post Helpful? 1
  • +
  • -

#6 worldofwar  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 40
  • Joined: 26-June 11

Re: Array checker?

Posted 10 August 2012 - 05:21 AM

It's a huge, messy class.. :whistling:

But thank you very much for explaining, much appreciated :)
Was This Post Helpful? 0
  • +
  • -

#7 Luckless  Icon User is offline

  • </luck>
  • member icon

Reputation: 291
  • View blog
  • Posts: 1,141
  • Joined: 31-August 09

Re: Array checker?

Posted 10 August 2012 - 05:26 AM

ahaha, okie dokie. Happy to help out. Feel free to come back
Was This Post Helpful? 1
  • +
  • -

#8 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 9039
  • View blog
  • Posts: 33,526
  • Joined: 27-December 08

Re: Array checker?

Posted 10 August 2012 - 12:27 PM

@Luckless: You can't use primitives with generics. You will have to use HashSet<Integer>.
Was This Post Helpful? 2
  • +
  • -

#9 Luckless  Icon User is offline

  • </luck>
  • member icon

Reputation: 291
  • View blog
  • Posts: 1,141
  • Joined: 31-August 09

Re: Array checker?

Posted 13 August 2012 - 10:01 AM

Excuse me, good call
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1