Skip to content

Instantly share code, notes, and snippets.

@apauley
Created June 10, 2012 20:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save apauley/2907182 to your computer and use it in GitHub Desktop.
Save apauley/2907182 to your computer and use it in GitHub Desktop.
Example list processing in Java
import java.util.*;
public class Transmogrify {
public static void main (String[] args) {
ArrayList<Integer> input = range(1, 10);
ArrayList<Integer> doubled = double_each(input);
ArrayList<Integer> complexified = complexify_each(input);
System.out.println(input);
System.out.println(doubled);
System.out.println(complexified);
}
public static ArrayList<Integer> double_each(ArrayList<Integer> inlist) {
ArrayList<Integer> out = new ArrayList<Integer>();
for(int i : inlist) {
out.add(i*2);
}
return out;
}
public static ArrayList<Integer> complexify_each(ArrayList<Integer> inlist) {
ArrayList<Integer> out = new ArrayList<Integer>();
for(int i : inlist) {
out.add(complexify(i));
}
return out;
}
public static int complexify(int x) {
return x * 4 + 3;
}
public static ArrayList<Integer> range(int start, int end) {
ArrayList<Integer> k = new ArrayList<Integer>();
for (int i=start; i<end; i++) {
k.add(i);
}
return k;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment