2

How should i declare a char array whose size ranges from 1 to 100 and I cannot make an array of size 100 because i have to make many arrays. My input is:

"bjomboleji";
"bnmjsjbfhaihfaihfga";
"zbihgfbjbnsdfbnbfkj";
"bnxbz";

and i have to check the common occurence of characters.

5
  • 1
    Please explain a bit more about your inputs. Commented Jul 20, 2014 at 16:53
  • If you're used to C or C++, then realize that Java very rarely uses character arrays. Strings are almost always preferred. Commented Jul 20, 2014 at 16:54
  • 2
    Your question is very unclear. At what point do you know how large the character array needs to be? Are you sure you need an array rather than a string anyway? Commented Jul 20, 2014 at 16:56
  • Noone told you about strings? Commented Jul 20, 2014 at 17:43
  • I have to check the presence of common characters in the inputs. Commented Jul 20, 2014 at 21:12

5 Answers 5

1

Use ArrayList for dynamic array.

List<Chracter> array = new ArrayList<Chracter>();
Sign up to request clarification or add additional context in comments.

Comments

1

Use a StringBuilder. It is a "mutable sequence of characters."

This is a better solution than a List<Character> as it avoids the need to create Character objects from char primitives.

This is a better choice than manipulating a String as String objects are immutable and manipulation results in additional objects being created.

2 Comments

it avoids the need to create Character objects from char primitives. have you heard about Autoboxing and Unboxing?
@Braj That is just syntactical sugar where the objects are created/obtained on your behalf. It does not avoid the actual creation/use of objects. The actual byte code results in calls to Character.valueOf.
0

Use a String to assign the value, then use toCharArray to convert to an array (if you really need a character array)

1 Comment

Yeah i do need to use each characters. And i cannnot possibly make character array for the whole 100 strings.
0

Variable length char is basically interface CharSequence which is implemented by String, StringBuilder, StringBuffer. So you can use any of it for variable char array.

Comments

0

To my understanding you need to count occurrences of a character in a string.If that's the case here is an example. there is no need to convert it to char array

public class Test {
public static void main(String[] args) {
    String x="dhakjkfhajfhuagjkadmnfd";
    String y="tskashguadmnsdm,as";

    String Check_Character="s";

    //Availability
    System.out.println("X has Check_Character :"+x.contains(Check_Character)); //false
    System.out.println("Y has Check_Character :"+y.contains(Check_Character));//true

    //Number of occurrences
    System.out.println("X has Check_Character :"+((x+" ").split(Check_Character).length-1)+" : times");//0 times
    System.out.println("Y has Check_Character :"+((y+" ").split(Check_Character).length-1)+" : times");//4times

} 
}

Otherwise you can use a list instead of an array or use this.

String z="dhakjkfhajfhuagjkadmnfd";
    char c[]=z.toCharArray();

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.