0

yesterday I appeared in interview,interviewer told me to write a code for calculating a length of array with out using a length property of Array class.

For examaple-

char[] array=new Scanner(System.in).nextLine().toCharArray();
// i have to write a code for calculating length of this array
//I can use any operator but use of library is restricted

All answer given here are using String library.

Community
  • 1
  • 1
  • in language like `c` we use while loop matched it with newline character `\n` to break but how is done with object. –  Jun 09 '15 at 07:32
  • 2
    use a try catch until you get an ArrayIndexOutOfBounds exception? – almightyGOSU Jun 09 '15 at 07:33
  • @Gosu. Appriciated yes i am also tried this, but this is not a good coding style to detrmine result by exception. –  Jun 09 '15 at 07:35
  • I hope there is a better way to do this (try/catch seems stupid indeed). – almightyGOSU Jun 09 '15 at 07:40
  • @gosu how does Array class evaluated its length property? since this `.class` file not available with src? –  Jun 09 '15 at 07:53
  • You can refer to this: http://stackoverflow.com/questions/9297899/where-is-arrays-length-property-defined – almightyGOSU Jun 09 '15 at 07:55

4 Answers4

2

A nicer solution might be to use the method: java.lang.reflect.Array::getLength

For example:

import java.lang.reflect.Array;

public class ArrayLength {
    public static void main(String[] args) {
        char[] array = new char[]{'a', 'b', 'a', 'c'};
        System.err.println(Array.getLength(array));
    }

}
gontard
  • 28,720
  • 11
  • 94
  • 117
1
char[] array = new Scanner(System.in).nextLine().toCharArray();
int count = 0;
for (int i : array) {
    count++;
}
System.out.println(count);
Sestertius
  • 1,367
  • 1
  • 14
  • 13
  • childish....great `for each loop` is simplest solution of this.very funny.why this not strict to us so far ,good job man. –  Jun 09 '15 at 08:06
  • 1
    In this case: The `length` property is used in the bytecode level: `arraylength` – gontard Jun 09 '15 at 08:26
  • @gontard thanks for giving such info,but should be expected since this is done by jdk not by us. –  Jun 09 '15 at 08:39
  • @dubey-theHarcourtians yes i guess. – gontard Jun 09 '15 at 08:41
0

try this:

    char []c = {'a', 'b', 'c'};
    int i = 0;
    int l = 0;
    try{
    while(c[i++] != 0)
    {
        System.out.println(c[i-1]);
        l++;
    }
    }catch(Exception a)
    {};
    System.out.println(l);
Diabolus
  • 268
  • 2
  • 15
0

As per my suggestion in the comments, which is definitely not good practice.

import java.util.Scanner;

public class QuickTester {

    public static void main(String[] args) {

        Scanner sc = new Scanner(System.in);

        System.out.print("Enter something: ");
        char [] charArr = sc.nextLine().toCharArray();

        int i = 0;
        try {
            while(true) {
                char c = charArr[i++];
            }
        }
        catch (ArrayIndexOutOfBoundsException e) {

        }

        System.out.println("Length: " + (i-1));
    }
}

Output:

Enter something: Banana
Length: 6
almightyGOSU
  • 3,731
  • 6
  • 31
  • 41
  • The bad practice is to not use the length property. This interviewer question is stupid. – gontard Jun 09 '15 at 07:40
  • @gontard Sometimes they just like to come up with trick questions? I mean it's not impossible to find the length without the length property after all. – almightyGOSU Jun 09 '15 at 07:41
  • @gontard Interviewer never be stupid,I think he was trying to know at what level of height u can think,idea was not to get a code for coding perspective.this was test of thinking outof box. –  Jun 09 '15 at 07:43
  • I didn't say the interviewer is stupid only its question... I think this kind of question doesn't help to recruit a good dev. – gontard Jun 09 '15 at 07:45