• Post Reply Bookmark Topic Watch Topic
  • New Topic

Question about String Arrays  RSS feed

 
Yvonne Wilkins
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I want to change some existing code that used to ask a simple comparison to now ask if something exists in the String array. (This is a totally made of situation)

How would I change the code below that used to ask if cat was not a Siamese to now ask if the cat is not either of the types in the string array?

I've come up with a solution but there's got to be a more efficient way because the string array of catType may grow. Can I address the string array .. catType .. directly? Should I put them in a List and loop through? Please advise. There is more to this code, however, I hope that I've provided enough information to get advice.


 
Tina Smith
Ranch Hand
Posts: 208
9
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is confusing...
What is Calico? Is it a String? An Object in a different class?
Where do Siamese, Persian, and Siberian come from on line 24? They look like the same objects as contained in the catType array but if that's what you're trying to get at, it won't compile.
Line 11 won't compile. You can't use boolean operators on a String.
A suggestion: Don't use == true. It should always be if( needCat) or if( !needCat).

If I follow your question, which I think occurs around line 24, you'd be better off putting into a List. List provides a .contains() method that will allow you to skip any looping entirely. So you could just write


And Welcome to the Ranch
 
Ted Smyth
Ranch Hand
Posts: 73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tina Smith wrote:This is confusing...
What is Calico? Is it a String? An Object in a different class?

I was about to ask the same thing! And to the OP, it's normally bad form to capitalize the first letter of an object instance (ie. private String Calico; //bad form, use calico instead). Because that is the convention for a Class or Interface name (ie. public class Calico). You can easily confuse variables/members with classes that way.

Tina Smith wrote:If I follow your question, which I think occurs around line 24, you'd be better off putting into a List. List provides a .contains() method that will allow you to skip any looping entirely. So you could just write

OP: Using this method you need not worry about how checking for existence of the String occurs (however, an ArrayList implementation of List will simply loop through the internal collection using the equals method against each item in the list).

Tina Smith wrote:And Welcome to the Ranch

Yes, welcome!
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yvonne Wilkins wrote:I want to change some existing code that used to ask a simple comparison to now ask if something exists in the String array. (This is a totally made of situation)

As the others have said, there are many ways to do what you want, but two spring to mind:

The first (my preference) is to make them classes, viz:Then you can use the instanceof operator, viz:
if (cat instanceof Siamese || cat instanceof Persian || ... ) { ...

The second is to use an enum of Cat types, and have each Cat instance know it's own type, eg:and then the check would be something like
if (cat.type() == Cat.Type.Siamese || cat.type() == Cat.Type.Persian || ... ) { ...
and the nice thing about enums is that you can use a switch statement if you have a lot of them to check.
However, the whole setup has a rather "procedural feel" to it.

But whatever you do, get away from using Strings. They make very poor substitutes for proper classes.

If you absolutely have to deal with a String array of possible types, the enum option may be a bit easier to convert. It may also be able to handle cross-breeds more easily (see EnumSet).

However, the great advantage of the first solution is that you can use polymorphism to implement breed-based behaviour without the need for any 'if' statements.

Hope all the above hasn't confused you too much .

Winston
 
dennis deems
Ranch Hand
Posts: 808
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Winston Gutkowski wrote:
The first (my preference) is to make them classes, viz:Then you can use the instanceof operator, viz:
if (cat instanceof Siamese || cat instanceof Persian || ... ) { ...

In my opinion, creating subtypes of Cat is appropriate only if there are different behaviors to implement. For this problem, all we want to do is categorize, so I think the enum attribute is a much better solution.


But whatever you do, get away from using Strings.

This.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!