the world is a pixel
Iterating over a HashMap
Iterating over a HashMap using the enhanced loop (foreach) in Java is a good way to keep your code smaller, more legible and usually more semantically coherent.
import java.util.HashMap; import java.util.Map; class Foo {} public class Main { public static void main(String args[]){ Map<Byte, Foo> mHash; mHash = new HashMap<Byte, Foo>(); mHash.put((byte)1, new Foo()); mHash.put((byte)2, new Foo()); mHash.put((byte)3, new Foo()); for(Foo f: mHash.values()){ System.out.println(f.toString()); } } }
No comments yet.
No trackbacks yet.
Java: invoking a method by name
29 April, 2010 - 10:29 am
Tags: Class, Java, method, programming, Reflection
Posted in english | 1 comment
import java.lang.reflect.*; public class Foo { public void bar(int param){ System.out.println(param); } public static void main(String args[]){ Object f = new Foo(); try { Method m = f.getClass().getMethod("bar", int.class); m.invoke(f, 42); } catch (Exception e){ System.err.println(e); } } } $ java Foo 42
calling commands in Java
8 April, 2010 - 1:09 pm
Tags: getruntime, Java, programming, runtime
Posted in english | No comments
I don’t like the approach of calling native shell commands in any language instead of using multi platform libraries, but here is a little prof of concept Java program to call native commands. import java.io.*; import java.util.*; public class Exec { public static void main(String args[]) throws IOException { Process proc = Runtime.getRuntime().e xec(args); BufferedReader [...]
Getting an Android app source
18 March, 2010 - 12:00 am
Tags: Android, branch, cmd, git, programming
Posted in english | 1 comment
Getting the Android’s AlarmClock application source from official repositories: git clone git://android.git.kernel.org/platform/packages/apps/AlarmClock.git To get the head version for an old platform like the 1.4 (codename donut), choose the correspondent branch using -o or –origin: git clone git://android.git.kernel.org/platform/packages/apps/AlarmClock.git –origin donut
Getting enviroment information on Android
16 March, 2010 - 10:18 am
Tags: Android, Eclipse, Java, programming, who am i, whoami
Posted in english | 4 comments
This is a simple program I wrote called Who Am I that shows informations about the device which it is running. Which can be useful for developers and maybe advanced users. Download: WhoAmI.tar.bz2 – Eclipse project. It’s configured for Android platform 4 (1.6) but should work without problems in newer Android platform versions. WhoAmI.apk – [...]
The Caps Lock Java Socket Server
27 February, 2010 - 9:39 pm
Tags: caps lock, Java, programming, socket, upcase
Posted in english | 1 comment
Here is a simple server for those who are starting studying sockets or just needs a simple socket server example for reuse while writing your own behavior. Features: A client should enter a string and the server would answer the same string, with each symbol in up case, when possible. Default port at 8080. One [...]
Beware the locale
22 February, 2010 - 5:16 pm
Tags: development, i18n, Java, JUnit, locale, programming, String, teste unitário, toString, unit testing
Posted in english | No comments
Today I was programming a toString method for a class widely used in a application, using the very useful String.format that provides a C’s like printf formatter. @Override public String toString() { return String.format("VO[a: %.1f, b: %.1f, c: %.1f]", a, b, a+b); } %.1f means a float with one digit precision after the dot separator. [...]
Miojo Script
28 January, 2010 - 2:37 am
Tags: libnotify, Linux, miojo, notify, notify-send, programming, Shell Script, ubuntu
Posted in english | 1 comment
O pre-requisito é o notify-send, um utilitário de linha de comando do libnotify. No Ubuntu: sudo aptitude install libnotify-bin E aqui o script em si: sleep 5m; notify-send “aviso” “tirar o miojo do fogo” Pronto, depois de cinco minutos isso vai aparecer:
Easily Sortable Date and Time Representation
20 January, 2010 - 10:55 pm
Tags: date, datetime, ISO 8601, Jochen Voss, Markus Kuhn, programming, Python, representation, sort, sorting, time
Posted in english | No comments
I was looking for a date and time representation useful for registering stock quotes in a simple plain file. I found that the standard ISO 8601 is just the answer for this, it’s called “Data elements and interchange formats — Information interchange — Representation of dates and times”. Here is a example: 2010-01-20 22:14:38 There’s this [...]
Java Font List
5 January, 2010 - 6:10 pm
Tags: AWT, fonts, Java, JVM, Larabie, Larabie Fonts, programming
Posted in english | Comments Off
Here’s a program that lists fonts available in your JVM. You can also set the environment variable JAVA_FONTS to specify the font directory. import java.awt.GraphicsEnvironment; public class ListFonts { public static void main(String args[]){ GraphicsEnvironment e = GraphicsEnvironment.getLocalGraphicsEnvironment(); for(String font:e.getAvailableFontFamilyNames()){ System.out.println(font); } } } By using pipes you can count how many fonts you [...]
Python Fast XML Parsing
25 December, 2009 - 3:04 pm
Tags: dtd, expat, game, programming, pygame, Python, sax, schema, urllib, XML
Posted in english | 1 comment
Here is a useful tip on Python XML decoding. I was extending xml.sax.ContentHandler class in a example to decode maps for a Pygame application when my connection went down and I noticed that the program stop working raising a exception regarded a call to urlib (a module for retrieve resources by url). I noticed that [...]












