Bay 12 Games Forum

Please login or register.

Login with username, password and session length
Advanced search  
Pages: 1 ... 8 9 [10] 11 12 ... 796

Author Topic: if self.isCoder(): post() #Programming Thread  (Read 832234 times)

Max White

  • Bay Watcher
  • Still not hollowed!
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #135 on: January 08, 2012, 01:31:34 am »

I'm trying to think, what does Java do that C# doesn't? Apart from run on your microwave...

Well there is that thing where inner classes intrinsically have access to their outer class, but that is easy to work around. Na it should be pretty easy to translate all lessons from Java over to c#, and if not then people can just ask for help.

I guess the biggest problem would be people doing things the over complicated Java way, such as using for loops on their lists instead of a foreach loop.

Mego

  • Bay Watcher
  • [PREFSTRING:MADNESS]
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #136 on: January 08, 2012, 01:34:27 am »

Hey, I'm reading your tutorials. Granted, we've already covered quite a bit of this stuff in my class, but I find it's good to go back to basics every so often.

I'm trying to think, what does Java do that C# doesn't? Apart from run on your microwave...

Well there is that thing where inner classes intrinsically have access to their outer class, but that is easy to work around. Na it should be pretty easy to translate all lessons from Java over to c#, and if not then people can just ask for help.

I guess the biggest problem would be people doing things the over complicated Java way, such as using for loops on their lists instead of a foreach loop.

For loops have some advantages over foreach loops. For one (no pun intended), you have your index right there, instead of having to use indexOf, which will only always work properly when used with sets.

Max White

  • Bay Watcher
  • Still not hollowed!
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #137 on: January 08, 2012, 01:36:37 am »

And in the case that you require the index, you can use a for loop.
Still, being able to run a foreach on your own data tree is pretty sexy.

Stargrasper

  • Bay Watcher
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #138 on: January 08, 2012, 02:23:55 am »

Thanks Mago.  Nice to know there are exactly two people reading my lessons. :P

I guess neither of you are familiar with the Java alternate for loop.  It's a foreach loop, basically.

Code: [Select]
for ( datatype instanceName : datastructure)
Code: [Select]
int[] num = { 1, 2, 3, 4, 5 };

for (int cur : num)
     System.out.println(cur);

Also works with objects.

Code: [Select]
List<Integer> numbers = new ArrayList<Integer>();
for(int i = 0; i < 5; i++) {
numbers.add(i);
}
for(Integer cur : numbers) {
System.out.println(cur);
}
Logged

Max White

  • Bay Watcher
  • Still not hollowed!
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #139 on: January 08, 2012, 02:26:23 am »

Also works with objects.

Code: [Select]
List<Integer> numbers = new ArrayList<Integer>();
for(int i = 0; i < 5; i++) {
numbers.add(i);
}
for(Integer cur : numbers) {
System.out.println(cur);
}

That works? What interface do I need to implement to make it work for my own stuff?

Stargrasper

  • Bay Watcher
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #140 on: January 08, 2012, 02:35:19 am »

Also works with objects.

Code: [Select]
List<Integer> numbers = new ArrayList<Integer>();
for(int i = 0; i < 5; i++) {
numbers.add(i);
}
for(Integer cur : numbers) {
System.out.println(cur);
}

That works? What interface do I need to implement to make it work for my own stuff?

Java does not make you implement any interfaces.  Just import the objects you need; in my example, you need to import List and ArrayList.  Apparently Integer comes free without having to import it.  The syntax for both for loops also comes free without imports/inheritance/interfaces.
Logged

Max White

  • Bay Watcher
  • Still not hollowed!
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #141 on: January 08, 2012, 02:45:11 am »

Yes I know there are collections you can just use, but what if I want to use my own? So, for example...
Code: [Select]
Level level = LevelFactory.BuildTestLevel();
for (Actor a : level)
{
a.Update();
}

In this example I am using a level object that I made myself as a wrapper for the actors that would be part of a game. But this doesn't just work as is, the Level class would need to implement an interface. Do you know what interface?

MorleyDev

  • Bay Watcher
  • "It is not enough for it to just work."
    • View Profile
    • MorleyDev
Re: if self.isCoder(): post() #Programming Thread
« Reply #142 on: January 08, 2012, 02:50:10 am »

Iterable: http://docs.oracle.com/javase/1.5.0/docs/api/java/lang/Iterable.html
"Implementing this interface allows an object to be the target of the "foreach" statement."
« Last Edit: January 08, 2012, 02:52:45 am by MorleyDev »
Logged

Stargrasper

  • Bay Watcher
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #144 on: January 08, 2012, 02:55:06 am »

Yes I know there are collections you can just use, but what if I want to use my own? So, for example...
Code: [Select]
Level level = LevelFactory.BuildTestLevel();
for (Actor a : level)
{
a.Update();
}

In this example I am using a level object that I made myself as a wrapper for the actors that would be part of a game. But this doesn't just work as is, the Level class would need to implement an interface. Do you know what interface?

For your excerpt to work, I think Level would need to implement Collection<T> or Iterable<T>.  Implementing Collection implies implementing Iterable as a superinterface.
Logged

Max White

  • Bay Watcher
  • Still not hollowed!
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #145 on: January 08, 2012, 02:58:47 am »

Yea but Java doesn't support multiple class inheritance, and I program to interfaces when ever possible anyway. No point extending Collection.

Stargrasper

  • Bay Watcher
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #146 on: January 08, 2012, 03:01:56 am »

Yea but Java doesn't support multiple class inheritance, and I program to interfaces when ever possible anyway. No point extending Collection.

Java considers Collection to be an interface...and implementing Collection implies implementing Iterable because Collection itself implements Iterable...and you can implement as many interfaces as you want in Java anyway.
Logged

Max White

  • Bay Watcher
  • Still not hollowed!
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #147 on: January 08, 2012, 03:04:25 am »

Collection is an interface? I thought it was an abstract class.
Well that works.

Stargrasper

  • Bay Watcher
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #148 on: January 08, 2012, 03:08:08 am »

Collection is an interface? I thought it was an abstract class.
Well that works.

I actually looked that up before writing that earlier post.  Collection is an interface.  Collections is an abstract class full of static methods that only work on things that implement Collection.
Logged
Pages: 1 ... 8 9 [10] 11 12 ... 796