Bay 12 Games Forum

Please login or register.

Login with username, password and session length
Advanced search  
Pages: [1] 2

Author Topic: I've a question for our local Java programmers.  (Read 2611 times)

Soulwynd

  • Bay Watcher
  • -_-
    • View Profile
I've a question for our local Java programmers.
« on: November 10, 2010, 07:15:54 pm »

Not sure if this would be the right forum for this, but the search tool indicated people have asked programming questions here and not in the creative projects forum, since this isn't really a creative project, just need a little bit of help.

So, the last time I programmed in java was like 10 years ago and I'm currently making a plugin to work with a minecraft mod. I've run into these Java collection methods (List, ArrayList, Hashtable, etc) and normally, I'd simply go with an array, struct (if this was c/c++), or even a class for each entry and an array with the pointers to control them. My major issue is that the documentation is -horrible- simply horrible. The online tutorials are also extremely ugly and non-helpful everywhere I go. If this was php or C it would be done already. I cannot even find anything on java arrays being dynamic or not, everywhere I see them people initialize them and that's not something I want. Anyway, off to the issue at hand:

I have a data set I need to manipulate, it should have a non-static ID entry simply to be able to point at a specific entry via an ingame command, since the data itself can be very similar. Each entry itself will have different values for: string playername, time, int blockid, int quantity, int price, and things like that. I need to be able to find entries via any of the variables, be able to delete any entry and possibly resort it with new IDs. The id in this case would simply be an int variable that's assigned whenever the database is loaded, I wouldn't simply use the array identifiers since I have no idea how java collections behave in that matter when deleting/adding entries.

What I had in mind was making a Hashtable or one of those to point to a class with each element, like:

public Hashtable<int, Classname> dataBase = new Hashtable<int, Classname>();

But since I have no experience with these java collections, I'm not sure how feasible it would be to search for elements inside the 'Classname' such as a player name. The int there would work as the ID for each game session. Plus I'm not sure how deleting/inserting elements behave with these collections. Since I also have no decent documentation on java's arrays, that isn't very helpful either. I suppose I could do a:

Classname[] dataBase;

But I don't know if I need to initialize it with a set length or if I can leave it dynamic, how deleting elements would work, what packes would I need to import for what I want to do, and things like that.


Just to be clear, I'm not looking for programming tutorials, since I already know how to program, just have a few questions about this in java. If you can point to a decent reference page that has programming examples that would help. If you can give me programming examples for what I want to do, maybe with different methods, that would help even more.

Thanks in advance. :)
Logged

ductape

  • Bay Watcher
  • MAD BOMBER
    • View Profile
    • Alchemy WebDev
Re: I've a question for our local Java programmers.
« Reply #1 on: November 10, 2010, 10:35:35 pm »

a dummy question here, but can Java work with MySQL? I think it can, from memory. That might be nice, since minecraft is running on a web server anyway. If you are better in php, maybe you can write it in PHP and send triggers from a tiny Java app.

TOTAL RUBE GOLDBERG!
Logged
I got nothing

Soulwynd

  • Bay Watcher
  • -_-
    • View Profile
Re: I've a question for our local Java programmers.
« Reply #2 on: November 10, 2010, 11:45:53 pm »

You're just trying to double my work, ductape. =p

If someone hints me a good way to work with data in java, I'm set.
Logged

Eagleon

  • Bay Watcher
    • View Profile
    • Soundcloud
Re: I've a question for our local Java programmers.
« Reply #3 on: November 11, 2010, 12:57:26 am »

Basic, built-in arrays (made with []) in Java are not resizable or dynamic. You need some descendant of a collection abstract, or another class that forms a collection - the whole mishmash becomes a lot more sensible once you realize that these aren't independent classes, but reimplement their unusable parent's basic methods. Once you realize that, the documentation becomes a lot more readable. For each collection class, you're only going to be using that class's methods to keep consistency with its internal structure.

This should be a good place to start to get a handle on it. There's a lot available. From your description, it sounds like the simplest way to implement it would be a HashMap. Each object stored within the HashMap (using HashMap.add()) should have methods inside it to return variables you want to search for, and you'd use the HashMap.values() method to get a list of hashes inside that map to iterate over, calling and testing against the appropriate Getter each time.

I don't know of a better way to do this, though there probably is. If your objects were guaranteed to have unique values for each variable, you could make separate HashMaps with associated keys, and reach each object very quickly, but I highly doubt that's the case, and anyway that might be pretty memory expensive.
« Last Edit: November 11, 2010, 12:59:58 am by Eagleon »
Logged
Agora: open-source, next-gen online discussions with formal outcomes!
Music, Ballpoint
Support 100% Emigration, Everyone Walking Around Confused Forever 2044

Sidhien

  • Bay Watcher
    • View Profile
Re: I've a question for our local Java programmers.
« Reply #4 on: November 11, 2010, 01:24:29 am »

This is coming from someone who's taken a whopping total of 1.5 java courses, (so take it with a grain of salt) but it sounds like you just want a plain ol' array list. Array lists are basically just dynamic arrays. They're accessed a little differently:

        'arrayListObject.get(0)' as opposed to 'array[0]'

but its still the same in how it can be used. It works with extended for loops so you can do something like:

ArrayList<EntryClass> chickenOfDoom = new ArrayList<EntryClass>();
public boolean nameMatches(String name)
{
      for (EntryClass o : chickenofDoom)
      {
             if ( o.playerName.equals( n ) )
                     return true;
      }
      return false;
}

Edit: Obviously thats assuming that you don't have like a billion entries in the array list, or you'd definitely want to go with a hashmap.
« Last Edit: November 11, 2010, 01:33:33 am by Sidhien »
Logged

Soulwynd

  • Bay Watcher
  • -_-
    • View Profile
Re: I've a question for our local Java programmers.
« Reply #5 on: November 11, 2010, 10:14:45 am »

I will go with HashMap pointing to a class. Thanks Sidhien, but ArrayList has no key->element setting and that could get messy later since I have no idea if these collections resort themselves after a remove, if the gap they leave is filled (ie, the new element pointer resets) and so on.
Logged

darkflagrance

  • Bay Watcher
  • Carry on, carry on
    • View Profile
Re: I've a question for our local Java programmers.
« Reply #6 on: November 11, 2010, 12:04:28 pm »

I will go with HashMap pointing to a class. Thanks Sidhien, but ArrayList has no key->element setting and that could get messy later since I have no idea if these collections resort themselves after a remove, if the gap they leave is filled (ie, the new element pointer resets) and so on.

I know that if you call the ArrayList.remove(element or index) of an arraylist, no gap should be left, if that's what you're asking (i.e. it moves everything to the left after what is removed).

But other than that ArrayList does lack key functionality anyway.
Logged
...as if nothing really matters...
   
The Legend of Tholtig Cryptbrain: 8000 dead elves and a cyclops

Tired of going decades without goblin sieges? Try The Fortress Defense Mod

Soulwynd

  • Bay Watcher
  • -_-
    • View Profile
Re: I've a question for our local Java programmers.
« Reply #7 on: November 11, 2010, 12:50:16 pm »

Yeah, that's what I meant.

I'm going with HashMap<int, object> so I can keep a session-static key index and avoid any mess up from people using commands at the same time.

Brings me to another question, can I nest these? Like:

Hashmap<int, ArrayList<object>>
Logged

Virex

  • Bay Watcher
  • Subjects interest attracted. Annalyses pending...
    • View Profile
Re: I've a question for our local Java programmers.
« Reply #8 on: November 11, 2010, 02:52:08 pm »

Sure, HashMaps can take any kind of Object, including ArrayLists, plain old arrays, other HashTable descendant types, Vectors, Lists and whatever other esoteric types you can think off or cook up yourself. The trick behind HashMaps is that it'll return a reference to the object irrespective of the Object itself. Setting it's type only instructs the compiler (and the eventual program) to do some additional typchecking.
What's even better is that if you don't explicitly declare the key-value pair, it will be set to Object-Object and you can add any kind of Object (Though I'd suggest keeping the keys as a primitive type or a string, to avoid complications with searching. Enums are another option). The values will be returned as an Object when searching though so you will have to typecast the return value to the right Object type.
« Last Edit: November 11, 2010, 02:53:41 pm by Virex »
Logged

Soulwynd

  • Bay Watcher
  • -_-
    • View Profile
Re: I've a question for our local Java programmers.
« Reply #9 on: November 11, 2010, 03:28:58 pm »

That works. Yet another question, I'm trying this at the moment (haven't compiled yet):

int[] keys = bankStore.keySet().toArray(); <- Seems like HashMaps only return Sets and I didn't find any Enum for it.
int slots = 0;
for (int key : keys) {
   slots = slots + slotsBlock(key); <- Should I "this.slotsBlock(key);" or is that fine? I've seem java sources with and without "this." for a class function.
}

Now, for the "for ( : ) {}" format, any way I can directly use it with <key, element> Collections (or any other collection for that matter)? The java tutorial says so, but doesn't give any example.
Logged

Virex

  • Bay Watcher
  • Subjects interest attracted. Annalyses pending...
    • View Profile
Re: I've a question for our local Java programmers.
« Reply #10 on: November 11, 2010, 03:42:36 pm »

Documentation for sets. You could probably use a while loop with an iterator:

Code: [Select]
Iterator KeyIt = bankStore.keySet().getIterator();
int slots = 0;



while(KeyIt.hasNext)
{
      slots += slotsBlock(KeyIt.next());
}

KeyIt.next() should return an int (or whatever you're using as the key type), but if you get an error in that line, try typecasting it to the correct type.
« Last Edit: November 11, 2010, 03:44:33 pm by Virex »
Logged

Eagleon

  • Bay Watcher
    • View Profile
    • Soundcloud
Re: I've a question for our local Java programmers.
« Reply #11 on: November 11, 2010, 03:52:12 pm »

Quote
<- Should I "this.slotsBlock(key);" or is that fine? I've seem java sources with and without "this." for a class function.
By calling the method with 'this.', you're calling slotsBlock() as an instance method, that is, a method that is not static. If the method is static, either way works, and omitting 'this.' is sensible - otherwise, reading it later, you might infer that it's an instance method where it is not. I'm not sure if you can overload static methods with an instance method, and this is the only case I can think of where it would matter, but I'm by no means an expert.

Basically, if the slotsBlock() method accesses variables local to 'this' object, you're doing things properly.
Logged
Agora: open-source, next-gen online discussions with formal outcomes!
Music, Ballpoint
Support 100% Emigration, Everyone Walking Around Confused Forever 2044

Soulwynd

  • Bay Watcher
  • -_-
    • View Profile
Re: I've a question for our local Java programmers.
« Reply #12 on: November 11, 2010, 04:02:44 pm »

Thanks Virex, Eagleon.

See, the '+=' operator was another thing I couldn't find in the documentation/tutorial -_-...
Logged

Virex

  • Bay Watcher
  • Subjects interest attracted. Annalyses pending...
    • View Profile
Re: I've a question for our local Java programmers.
« Reply #13 on: November 11, 2010, 04:10:07 pm »

Logged

Soulwynd

  • Bay Watcher
  • -_-
    • View Profile
Re: I've a question for our local Java programmers.
« Reply #14 on: November 11, 2010, 04:17:11 pm »

Man, and I was looking here...

I really dislike the java tutorial site. -_-

Makes me miss both D and PHP documentation.
Logged
Pages: [1] 2