Created
April 17, 2011 22:47
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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