ananymous59
ananymous59

Reputation: 200

Get length of an array

I am trying to extract vowels from a string and find the count of those vowels. The counter that I declared is working fine, but the array length is always the same. Can someone explain this behavior?

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

public class Vow {
    static int count = 0;
    String arr[] = new String[40];

    public static char[] getV(char[] list) {
        List<Character> a = new ArrayList<Character>();
        char[] vow = new char[30];
        char[] soluc = null;
        int count = 0;

        if (list==null) {
            System.out.println("You are passing null value");
            return null;
        }

        for (int i = 0; i < list.length - 1; i++) {
            if (isV(list[i])) {
                a.add(list[i]);
                vow[i] = list[i];
                count ++;
            }
        }

        System.out.println("no of vowels is " + count);//this works
        System.out.println("size is " + vow.length);
        System.out.println("vowels are " + String.valueOf(vow));
        System.out.println("size is " + vow.length);//but this does not why ?
        System.out.println("ar is " + a);
        return vow; 
    }

    public static boolean isVowel(char c) {
        String vowels = "aeiouAEIOU";
        return vowels.indexOf(c) >= 0; //c exists in the String vowels
    }

    public static void main(String[] args) {
        String s = "harestiou";
        char[] a = s.toCharArray();
        char[] b = getV(a);
        System.out.println(b);
        System.out.println(b.length);//it is not showing the length of vowels                             but instead the original size it was allocated
    }
}

Upvotes: 0

Views: 95

Answers (3)

Arkantos
Arkantos

Reputation: 6608

System.out.println("size is"+vow.length);//but this does not why ?

That's because of this char []vow=new char[30]; You created an array of fixed length and then in the if condition, you're just updating the elements of that array, so length will remain same in fact even if you didn't change the contents of that char array, vow.length will always be 30 because that's the size you created it with.

If you need a char[], then you should manually copy the elements because existing methods deal with only copying of Objects->Objects but you need primitive char[] array, so change your code like this just before you return vow

I seriously suggest changing your List<Character> variable name to charList or something meaningful instead of just single letter names like a

// in the method starting
List<Character> charList = new ArrayList<Character>();
// do your logic of checking vowels and updating charList
char [] vowels = new char[charList.size()];

for(int i=0; i<charList.size() ; i++){
   vowels[i] = charList.get(i);
}
return vowels;

Upvotes: 1

aberry
aberry

Reputation: 447

Limiting vowel array of size 30 is not optimum solution. If input string have vowel count more than 30 , then it will fail. I made few changes in getV function & used StringBuffer instead of duplicate usage of variable and array. Although there could be further optimization !!!

 public static char[] getV(char[] list) {

   // No need of these extra arrays. so commenting and using single   StringBuffer to hold vowels.
    //List<Character> a=new ArrayList<Character>();
    //char []vow=new char[30];
    //char[] soluc=null;
    StringBuffer sb = new StringBuffer();



    int count=0;

    if(list==null)
    {
        System.out.println("You are passing null value");
        return null;
    }

    for(int i=0;i<list.length-1;i++)
    {
        if(isVowel(list[i])==true)
        {

            sb.append(list[i]);
                    count ++;

        }


    }


    System.out.println("no of vowels is"+count+"");//this works
    System.out.println("size is" + sb.toString().length());
    System.out.println("vowels are  "+sb.toString());
    System.out.println("size is "+sb.toString().length());//but this does not why ?
    System.out.println("ar is  "+sb.toString());

    return sb.toString().toCharArray();

}

Hope this help. (Although there could be further optimization in logic!!!)

Upvotes: 1

GhostCat
GhostCat

Reputation: 140427

I think your problem is that you expect that .length should return the number of "real elements" of an array.

That is not what happens: when you create an array with size 30, like

someArray =new char[30] ... 

then someArray.lenght always returns 30; no matter how many "slots" in that array you actually populated. You have to turn the array into a String for example in order to retrieve the actual number of characters.

Upvotes: 2

Related Questions