Untitled
supelpawel
java
3 years ago
1.6 kB
17
Indexable
package pl.coderslab;
import java.util.ArrayList;
import java.util.List;
public class Main06 {
public static void main(String[] args) {
int[] numbers = new int[]{1, 3, 6, 2, 9};
System.out.println(findTwoNumbersOfSum(numbers, 15));
}
public static String findTwoNumbersOfSum(int[] tab, int index) {
List<Integer> tempList1 = new ArrayList<>();
List<Integer> tempList2;
int firstInt = 0;
int secondInt = 0;
for (int i : tab) {
tempList1.add(i);
}
for (int i = 0; i <= tempList1.size() - 1; i++) {
int tempInt = tempList1.get(i);
tempList2 = new ArrayList<>();
for (int j : tempList1) {
if (j != tempInt) {
tempList2.add(j);
}
}
for (int j = 0; j <= tempList2.size() - 1; j++) {
if (tempInt + tempList2.get(j) == index) {
firstInt = tempInt;
secondInt = tempList2.get(j);
}
}
}
int indexFirstInt = 0;
int indexSecondInt = 0;
for (int i = 0; i <= tab.length -1; i++) {
if (tab[i] == firstInt) {
indexFirstInt = i;
}
if (tab[i] == secondInt) {
indexSecondInt = i;
}
}
return String.format("First number: %d with index %d, second number: %d with index %d", firstInt, indexFirstInt, secondInt, indexSecondInt);
}
}
Editor is loading...