Skip to content

Instantly share code, notes, and snippets.

@tautologico
Created April 17, 2011 22:47
Show Gist options
  • Save tautologico/924558 to your computer and use it in GitHub Desktop.
Save tautologico/924558 to your computer and use it in GitHub Desktop.
Exemplo de como gerar uma lista que contem a soma dos pares em outra lista em Java
import java.util.Iterator;
import java.util.List;
import java.util.ArrayList;
public class SomaPares {
public static List<Integer> somapares(List<Integer[]> pares)
{
List<Integer> result = null;
if (pares != null)
{
result = new ArrayList<Integer>();
for (Integer[] par : pares)
{
if (par.length != 2)
throw new IllegalArgumentException("Lista contem elemento que não é um par.");
result.add(par[0] + par[1]);
}
}
return result;
}
public static void main(String[] args)
{
ArrayList<Integer[]> pares = new ArrayList<Integer[]>();
pares.add(new Integer[] {1, 5});
pares.add(new Integer[] {4, 7});
List<Integer> soma = SomaPares.somapares(pares);
System.out.print("[");
int c = 0;
for (Integer i : soma)
{
if (c != 0)
System.out.print(", ");
c++;
System.out.print("" + i);
}
System.out.print("]");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment