-1

Original Question:

write the following method that returns a new ArrayList. The new list ONLY contains the vowel elements from the original list which contains an array of English characters (a,e,i,o,u).

This is what i have so far. I believe i created the right generic method to print out my Array List of characters I'm just struggling on how to put the vowels into another array and print it. Any suggestions? I think i may need to create a generic ArrayList of "vowels" to make it run? I'm i on the right track?

Thanks for any help!

Here's my code so far:

import java.util.*;

public class test5 {

    public static void main(String[] args) {

    Character [] letters ={'a', 'r', 'y', 'i', 'i', 'o', 's', 'p', 'q', 'x', 'd', 'e', 'j'};
    Character [] vowels ={};

    test5.print(letters);
    test5.print(vowels);      

    }  

 public static <E> void print(E[] list) {
 for (int i=0; i < list.length; i++)
     System.out.print(list[i] + "");
     System.out.println();

    }

public <E> void vowels(E[] list){

 for (int i=0; i < list.length; i++)

     if (i=='a')
         vowels.add(i);

     else if (i=='e')
         vowels.add(i);

}

public static <E> void vowelsArray(E[] list){

     for(int i = 0; i<vowels.size(); i++)
       System.out.print(vowels.get(i));

}

}
David L.
  • 2,095
  • 23
  • 23
user1789951
  • 661
  • 3
  • 7
  • 12

1 Answers1

1

It sounds like the point of this assignment is to teach you to use a List (an ArrayList is a type of List).

So far you're not using an ArrayList at all: you're using arrays.

There is a big difference.

Lists are helpful because, unlike arrays, they can grow dynamically.

Your teacher wants you to iterate over the letters in your input and to add each vowel to a List.

You should start by taking a look at the JavaDoc for List and ArrayList:

The way your code is right now, there's no need for generics.

You really don't need a generic method either.

You want a method that returns a typed List.

Here's a hint... This is how you create a List of Character objects:

List<Character> characterList = new ArrayList<Character>();

jahroy
  • 22,322
  • 9
  • 59
  • 108