473,403 Members | 2,366 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,403 software developers and data experts.

Searching arrays?

Is there a better way to do this:
Expand|Select|Wrap|Line Numbers
  1. // attackable[] is a array of Country objects. it is variable in length
  2.     public boolean canAttack(Country c) 
  3.     {
  4.         int x;
  5.         for(x = 0; (attackable[x] != c)&&(x < attackable.length); x++){};
  6.         if((x < attackable.length) && (attackable[x] == c)) 
  7.         {
  8.             if(c.getOwner() == owner)
  9.                 return false;
  10.             else
  11.                 return true;
  12.         } else
  13.             return false;
  14.     }
  15.  
That's a method that I'm working on for my compsci class... anyone know a more efficient way to do that? mainly just the
Expand|Select|Wrap|Line Numbers
  1.         int x;
  2.         for(x = 0; (attackable[x] != c)&&(x < attackable.length); x++){};
  3.         if((x < attackable.length) && (attackable[x] == c)) 
  4.  
part, but any help or pointers with the rest would be appreciated too.
Sep 17 '08 #1
3 1181
Nepomuk
3,112 Expert 2GB
There is a better way to do it indeed. For example, you don't have to have that for loop run without doing anything. And see, it tests the same thing as the if command beneath.

Then, you check
Expand|Select|Wrap|Line Numbers
  1. if(c.getOwner() == owner) // shouldn't that be done with equals? (see below)
and then return the opposite of that answer - that can be shortened a lot. Just have a look at this:
Expand|Select|Wrap|Line Numbers
  1. public boolean not(boolean myBoolean) {
  2.     return !myBoolean;
  3. }
It will always return the opposite of the boolean you gave - do you understand why?

Also, remember that as soon as you return a value, the rest of the code will not be run. So you can leave out some tests.

Now, isn't Country a own class? If so, you should compare it with
Expand|Select|Wrap|Line Numbers
  1. if(attackable[x].equals(c)) {...
Similar with the owner - if that's anything but a primitive data type, you should compare that with equals too.

Greetings,
Nepomuk
Sep 18 '08 #2
Lol, thanks :) I wrote that at like, midnight :P

Question, do I have to write my own equals method, or does java do that automatically for me?
Sep 18 '08 #3
Nepomuk
3,112 Expert 2GB
Lol, thanks :) I wrote that at like, midnight :P

Question, do I have to write my own equals method, or does java do that automatically for me?
You should write your own - this article should help you do so.

Greetings,
Nepomuk
Sep 19 '08 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: Bryce Maschino | last post by:
hello, i am trying to make a java program that will take several different arrays and search through them in a loop suchs as: for (i=0; i < 5; i++) { if (firstarray<such and such { blah blah
1
by: Scott Handojo | last post by:
Lets say I have the following arrays X1 (1, 2, 7, 8) X2 (1, 2, 4, 6) X3 (1, 2, 3, 5) X4 (1, 3, 5) X5 (2, 3, 5) X6 (4, 5, 6, 8) My 'search array' is comprised of
1
by: Bud Dean | last post by:
I need to search files for given text. In particular, I'm searching dll's, exe's, asp, aspx and html pages. I am having difficulty converting the byte arrays to strings. The following code...
1
by: BlackJackal | last post by:
Alright here is the problem I have for homework. I understand most of it but I am not exactly sure what the problem is asking me to do or how to search the seperate arrays using the account number...
5
by: lemlimlee | last post by:
hello, this is the task i need to do: For this task, you are to develop a Java program that allows a user to search or sort an array of numbers using an algorithm that the user chooses. The...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.