Код: Выделить всё
import java.util.ArrayList;
import java.util.List;
public class Calculate {
public static List converter(List list) {
List doubleList = new ArrayList();
for (String s : list) {
doubleList.add(Double.parseDouble(s));
}
return doubleList;
}
public static Double addition(Double a, Double b) {
return a + b;
}
public static Double subtraction(Double a, Double b) {
return a - b;
}
public static Double multiplication(Double a, Double b) {
return a * b;
}
public static Double division(Double a, Double b) {
return a / b;
}
public static Double operation(List doubles1, List doubles2, List symbols, Double result) {
Double a = 0.0;
Double b = 0.0;
for (int i = 0; i < doubles1.size(); i++) {
a = doubles1.get(i);
for (int j = 0; j < doubles2.size(); j++) {
b = doubles2.get(j);
if (symbols.get(i).equals("+")) {
result = Calculate.addition(a, b);
} else if (symbols.get(i).equals("-")) {
result = Calculate.subtraction(a, b);
}
}
}
return result;
}
public static void main(String[] args) {
Double result = 0.0;
List doubles = new ArrayList();
List doubles2 = new ArrayList();
doubles.add(33.0);
doubles2.add(17.0);
doubles.add(50.0);
doubles2.add(50.0);
doubles.add(50.0);
List symbols = new ArrayList();
symbols.add("+");
symbols.add("+");
symbols.add("-");
symbols.add("-");
System.out.println(operation(doubles, doubles2, symbols, result));
}
}
< /code>
Посмотрите на мой класс для добавления списка: < /p>
import java.util. ; Импорт java.util.concurrent. < /em>; < /p>
public class test2 {
private static double -результат = 0,0;
Private Static Double Double = 0,0; < /p>
private static final List firstValues = Collections.synchronizedList(new ArrayList());
private static final List symbols = Collections.synchronizedList(new ArrayList());
private static final List secondValues = Collections.synchronizedList(new ArrayList());
private static final BlockingQueue q1 = new LinkedBlockingQueue();
private static final BlockingQueue qs = new LinkedBlockingQueue();
private static final BlockingQueue q2 = new LinkedBlockingQueue();
private static volatile boolean running = true;
public static void main(String[] args) throws InterruptedException {
Scanner scanner = new Scanner(System.in);
// Reader thread: routes inputs to correct queues
Thread reader = new Thread(() -> {
while (running) {
String input = scanner.nextLine();
if (input.equals("=")) {
running = false;
q1.offer("STOP");
qs.offer("STOP");
q2.offer("STOP");
break;
}
if (input.startsWith("1:")) {
q1.offer(input.substring(2));
} else if (input.startsWith("s:")) {
qs.offer(input.substring(2));
} else if (input.startsWith("2:")) {
q2.offer(input.substring(2));
} else {
System.out.println("Invalid input. Use 1:, s:, or 2");
}
}
});
// Each worker reads only from its own queue
Thread worker1 = new Thread(() -> process(q1, firstValues, "1st"));
Thread workerS = new Thread(() -> process(qs, symbols, "Symbol"));
Thread worker2 = new Thread(() -> process(q2, secondValues, "2nd"));
reader.start();
worker1.start();
workerS.start();
worker2.start();
reader.join();
worker1.join();
workerS.join();
worker2.join();
}
private static void process(BlockingQueue queue, List list, String name) {
while (running || !queue.isEmpty()) {
try {
String value = queue.poll(100, TimeUnit.MILLISECONDS);
if (value == null || value.equals("STOP")) continue;
list.add(value);
System.out.println("[" + name + "] Added: " + value);
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
}
}
} }
Подробнее здесь: https://stackoverflow.com/questions/795 ... t-elements