Java - Iterable

 
Vista:
Imágen de perfil de Juan José
Val: 54
Ha mantenido su posición en Java (en relación al último mes)
Gráfica de Java

Iterable

Publicado por Juan José (20 intervenciones) el 13/12/2020 20:29:52
Tengo este código en un JUnit5, tengo una clase de Sujetos, sujeto1 es una instancia de esta clase.
el problema que tengo es que tengo que recorrerlo.
El error que me da el test es:

java.lang.Error: Unresolved compilation problem:
Can only iterate over an array or an instance of java.lang.Iterable

1
2
3
4
ArrayList<Pair<String, ArrayList<Double>>> notas = new ArrayList<Pair<String, ArrayList<Double>>>();
for (Pair<String, ArrayList<Double>> par : sujeto1) {
	notas.add(par);
}

La clase Sujeto sería esta:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
package org.eda1.actividad03;
 
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Iterator;
import java.util.Locale;
 
public class Subject {
 
	// code = @nombreSujeto
	// testPuntuation = "nombrePrueba" : "[double, double...]"
	private String code;
	private BSTreeMap<String, ArrayList<Double>> testsPunctuations; // par<testName, punctuations>
 
	public Subject(String code) {
		this.code = code;
		this.testsPunctuations = new BSTreeMap<String, ArrayList<Double>>();
	}
 
	public boolean add(String testName, Double... punctuations) {
		ArrayList<Double> values = new ArrayList<Double>(Arrays.asList(punctuations));// haced uso de Arrays.asList()
		ArrayList<Double> current = testsPunctuations.get(testName);// ...
		if (current != null)
			this.testsPunctuations.get(testName).addAll(current);
		else
			testsPunctuations.put(testName, values);// ...
		return current == null;
	}
 
	public String getMaxPunctuation() {
		double candidate = Double.MIN_VALUE;
		// 1 for() --> haced uso de Collections.max(array)
		// ...
		for (ArrayList<Double> it : this.testsPunctuations.values()) {
			candidate = Collections.max(it);
		}
		return String.format(Locale.US, "%.2f", candidate);
	}
 
	public String getMaxPunctuation(String testName) {
		if (this.testsPunctuations.get(testName) != null) {
			ArrayList<Double> punctuations = this.testsPunctuations.get(testName);
			return String.format(Locale.US, "%.2f", (Collections.max(punctuations)));
		}
		return null;
	}
 
	public void clear() {
		this.testsPunctuations.clear();
	}
 
	@Override
	public String toString() {
		// "@oswald=<1 prueba>"
		return this.code + "=<"
				+ ((this.testsPunctuations.keySet().size() == 1) ? this.testsPunctuations.keySet().size() + " prueba>"
						: this.testsPunctuations.keySet().size() + " pruebas>"); // ...
	}
 
}

¿Cómo soluciono este error en la clase Subject?
Valora esta pregunta
Me gusta: Está pregunta es útil y esta claraNo me gusta: Está pregunta no esta clara o no es útil
0
Responder
Imágen de perfil de Rodrigo
Val: 2.041
Plata
Ha mantenido su posición en Java (en relación al último mes)
Gráfica de Java

Iterable

Publicado por Rodrigo (623 intervenciones) el 14/12/2020 01:13:56
Como esta definida la variable sujeto1 ?
Valora esta respuesta
Me gusta: Está respuesta es útil y esta claraNo me gusta: Está respuesta no esta clara o no es útil
0
Comentar
Imágen de perfil de Juan José
Val: 54
Ha mantenido su posición en Java (en relación al último mes)
Gráfica de Java

Iterable

Publicado por Juan José (20 intervenciones) el 14/12/2020 10:42:25
Perdona Rodrigo, pensaba que había puesto la declaración, la dejo aquí abajo:

1
Subject sujeto1 = new Subject("@oswald");

Lo terminé resolviendo así:
Añadiendo esto en la cabecera de la clase:
1
public class Subject implements Iterable<Pair<String, ArrayList<Double>>> {
Siendo el método sobrescrito de Java
1
2
3
4
5
6
7
8
@Override
	public Iterator<Pair<String, ArrayList<Double>>> iterator() {
		ArrayList<Pair<String, ArrayList<Double>>> lista = new ArrayList<Pair<String,ArrayList<Double>>>();
		for (String p :this.testsPunctuations.keySet()) {
			lista.add(new Pair<String, ArrayList<Double>>(p, this.testsPunctuations.get(p)));
		}
		return lista.iterator();
	}
Valora esta respuesta
Me gusta: Está respuesta es útil y esta claraNo me gusta: Está respuesta no esta clara o no es útil
0
Comentar