Jump to content
AprendizZ

Mudar Switch/Case para ArrayList

Recommended Posts

AprendizZ

Boas, pretendo comparar a presença de vogais numa palavra e queria mudar um switch/case statement para um arraylist de forma a que a comparação fosse feita caso existisse alguma vogal da lista na palavra que estou a avaliar. Só que não sei como dar a volta.

É no seguinte código que pretendo mudar (no final):

import java.util.ArrayList;
import java.util.List;

public class SSP implements IState {
	private int cost; // cost from the receiver to its father
	private String s;

	public SSP(String str) {
		this(str, 0);
	}

	public SSP(String str, int cost) {
		s = str;
		this.cost = cost;
	}

	public boolean equals(Object that) {
		if (that instanceof SSP) {
			SSP b = ((SSP) that);
			return s.equals(b.s);
		} else
			return false;
	}

	public boolean isGoal(IState that) {
		return this.equals(that);
	}

	public String toString() {
		return s;
	}

	/**
	 * @return a list with all configuration states which are reachable from the
	 *         receiver, when the rules of our problem are applied.
	 */
	public List<IState> children() {
		int n;
		List<IState> l = new ArrayList<IState>();
		SSP child;
		for (int i = 0; i < s.length() - 1; i++)
			for (int j = i + 1; j < s.length(); j++) {
				StringBuffer schild = new StringBuffer(s);
				n = swap(schild, i, j);
				child = new SSP(schild.toString(), n);
				l.add(child);

			}
		return l;
	}

	public int getCost() {
		return cost;
	}

	private static int swap(StringBuffer s, int i, int j) {
		char c = s.charAt(i);
		char d = s.charAt(j);
		s.setCharAt(i, d);
		s.setCharAt(j, c);
		if (isVowel(c) && isVowel(d))
			return 200;
		if (isVowel(c) || isVowel(d))
			return 11;
		return 2;
	}

	private static boolean isVowel(char c) {
		switch (c) {
		case 'a':
		case 'e':
		case 'i':
		case 'o':
		case 'u':
			return true;
		}
		return false;
	}
}

Agradeço qualquer ajuda.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×
×
  • Create New...

Important Information

By using this site you accept our Terms of Use and Privacy Policy. We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.