java - Splitting the string and store it in an array -
i want following string stored array throws arrayoutofbound exception.
public static void main(string[] args) { // todo auto-generated method stub char[] arr = {}; string str = "hello name ivkaran"; (int = 0;i < str.length(); i++){ system.out.println(str.charat(i)); arr[i] = str.charat(i); } }
you've declared array of length 0 line:
char[] arr = {};
to assign array, must initialized non-zero size. here, looks need same size string.
string str = "hello name ivkaran"; char[] arr = new char[str.length()];
you can call tochararray()
on string
char[]
contents copied already.
char[] arr = str.tochararray();
Comments
Post a Comment