0

So I have an ArrayList which contains String values in my main method.

And I want to go and search in the enum class the reference of each element of my ArrayList and set it in the same index .

So lets say my arrayList is ["GUA", "AUG", "CUA"] I want to have in my output [Val, Met, Leu].

So I am trying to fix my find() method so that it returns an arrayList not just a string, and without using a 'for' loop in main().But it will always give me an error when I try to change my return type.

Any ideas?

public enum Abreviation {
    Ala("GCU", "GCC", "GCA", "GCG"),
    Arg("CGU", "CGC", "CGA", "CGG", "AGA", "AGG"),
    Asn("AAU", "AAC"),
    Asp("GAU", "GAC"),
    Cys("UGU", "UGC"),
    Glu("GAG", "GAA"),
    Gln("CAG", "CAA"),
    Gly("GGU", "GGC", "GGA", "GGG"),
    His("CAC", "CAU"),
    Ile("AUU", "AUC", "AUA"),
    Leu("UUA", "UUG", "CUU", "CUC", "CUA", "CUG"),
    Lys("AAA", "AAG"),
    Met("AUG"),
    Phe("UUU", "UUC"),
    Pro("CCU", "CCC", "CCA", "CCG"),
    Pyl("UAG"),
    Sec("UGA"),
    Ser("UCU", "UCC", "UCA", "UCG", "AGU", "AGC"),
    Thr("ACU", "ACG", "ACA", "ACC"),
    Trp("UGG"),
    Tyr("UAU", "UAC"),
    Val("GUU", "GUC", "GUA", "GUG"),
    Fin("UAA")
    ;

    private final List<String> arn;

    private Abreviation(final String... arn) {
        this.arn = Arrays.asList(arn);
    }


    public boolean contains(final String codon) {
        return this.arn.contains(codon);
    }

}





public class MainMain {

    public static void main(String[] args) {

        ArrayList <String> strings = new ArrayList<String>();

        strings.add("GUA");
        strings.add("AUG");
        strings.add("CUA");

        for (int i = 0; i < strings.size(); i++) {          
            System.out.println(find(strings.get(i), strings));          

        }


    }


    public static List<String> find(final String codon, List<String> strings) {             

        for (final Abreviation abb : Abreviation.values()){

            if (abb.contains(codon)) {
                int arn = strings.indexOf(codon);
                strings.set(arn, abb.name());

                return  strings;
            }
        }
        throw new IllegalArgumentException("Erreur");
    }


}

3 Answers 3

1

It is because you are printing the strings list everytime you call the find function.since there are three items in strings list, the strings list is being printed three times.Try this.

public static void main(String[] args) {

    ArrayList <String> strings = new ArrayList<String>();

    strings.add("GUA");
    strings.add("AUG");
    strings.add("CUA");
    System.out.println(find(strings));

}


public static List<String> find(List<String> strings) {             
    boolean flag = false;
    for( int i=0 ; i<strings.size() ; ++i ) {
        String codon = strings.get(i);
        for (final Abreviation abb : Abreviation.values()){
            if (abb.contains(codon)) {
                flag = true;
                int arn = strings.indexOf(codon);
                strings.set(arn, abb.name());
            }
        }
        if( !flag ) {
            throw new IllegalArgumentException("Erreur");
        }
    }
    return strings;
}
Sign up to request clarification or add additional context in comments.

2 Comments

I actually wanted to fix the find() method so it can return [Val, Met, Leu]
Then you can iterate items in the strings list in find function like above instead of iterating in main function.
0

It would be better to create a new list with String or Abbreviation, but if you want to do replacement you can use replaceAll.

strings.replaceAll(s -> Stream.of(Abreviation.values())
    .filter(x -> x.contains(s))
    .findFirst().orElseThrow(() -> new IllegalArgumentException("Erreur")).name()
);

Or without Stream :

strings.replaceAll(s -> {
    for (final Abreviation abb : Abreviation.values()){
        if (abb.contains(s)) {
            return abb.name();
        }
    }
    throw new IllegalArgumentException("Erreur");
});

1 Comment

that's what I was looking for. Thank you :)
0

Why don't you return a list of Abreviations?

Like this:

public static List<Abreviation> find(final String codon) {             
    List<Abreviation> result = new ArrayList<>();
    for (final Abreviation abb : Abreviation.values()){
        if (abb.contains(codon)) {
            result.add(abb);
        }
    }
    return result;
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.