• Post Reply Bookmark Topic Watch Topic
  • New Topic

How do I reduce the number of parrallel arrays in my code?  RSS feed

 
Raed Tabani
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm building a text based "game" where you are communicating with this android creature called Gargoid , at a VERY primitive level . how it works is, simply the user type in a sentence which is decoded for meaning by comparing it with a built in list of words in order to figure out what the user is saying, and then reply with a a relevant response also from a list of built in words.
the whole thing would look something like this,
user: what is your name
Gargoid : my name is Gargoid, nice to meet you
user: how is the weather
Gargoid: the weather is wonderful

so far I have 11 arrays which are the following

String[] for user typed in words used for comparison to find meaning
An Array of String[] , 7 so far, to hold what I call the Gargoid dictionary for example String[] greeting={hi,hello,aloha}, words that indicates greeting
int[] called frequency to determine which of the 7 arrays have the greatest "relevancy" to what is being said.
and finally another String[] for responses

here's the actual code, I want you guys to tell me if there's a way to reduce all this never ending number of arrays? and also is this code a good application of object oriented programming?

MainClass













 
Stevens Miller
Bartender
Posts: 1445
30
C++ Java Netbeans IDE Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Anytime you have two arrays of the same length, and the elements of each array form some kind of pair, you are better off creating a class of object that includes each pair as instance variables. Then, you can make an array of those objects. In your code, I would consider creating an object that included an array of Strings, and a single integer that kept the frequency for that array. Then, instead of your frequency array being paired with your gargoidDictionary array, you could have a single array with each element containing one of your arrays of Strings, and the frequency for that array.

There are probably ways to avoid all those String arrays, too, but I'll let someone else take a shot at that.
 
Darryl Burke
Bartender
Posts: 5167
11
Java Netbeans IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Looks to me more like a Map<String, List<String>> than a custom class.
 
Raed Tabani
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Stevens Miller wrote:Anytime you have two arrays of the same length, and the elements of each array form some kind of pair, you are better off creating a class of object that includes each pair as instance variables. Then, you can make an array of those objects. In your code, I would consider creating an object that included an array of Strings, and a single integer that kept the frequency for that array. Then, instead of your frequency array being paired with your gargoidDictionary array, you could have a single array with each element containing one of your arrays of Strings, and the frequency for that array.

There are probably ways to avoid all those String arrays, too, but I'll let someone else take a shot at that.


Hi Stevens Miller, I did that and I don't know if it better, but I love it not only I got to get rid of all these ugly String[] and allocate them to Class of their own, Dictionary, I was able to lose the frequency array as well, and most importantly, I think I found a way over the array length limitation. anyways, heres the new code and I would love to get your feedback





 
Raed Tabani
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
unfortunately Darryl Burke, I'm not familiar with maps yet. I guess this will be a good time to get to know them meanwhile, I would love to get your feedback on the new code
 
Knute Snortum
Sheriff
Posts: 4279
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
One thing: don't pass in data you can calculate, so for instance:



You can calculate wordFiller.length, so don't pass it in. Now your call can look like:

 
Campbell Ritchie
Marshal
Posts: 56546
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
For copying arrays consider two copying methods (1 2) which both take numbers so you can copy part of an array.
To copy the whole of an array consider that arrays's clone() method.
If you want to be swish, this only works in Java8:-
Arrays.stream(myArray).toArray();

…and it might not work at all if I have written it wrongly
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!