Reading one character at a time from a large string - java

I have a big string having at most 100000 character. Instead of using string.charAt[index] to read a character from the string, I converted that string into char array using string.toCharArray() method and now i am working with charArray[index]. which takes less time than string.charAt[index] method. However i want to know that, is there any other way which is faster than string.toCharArray(); method?

I do not think there is a faster way. But please correct me!
A String instance is backed by a char array. charAt() does some index checks which may be the cause for it being slower than working with the array returned by toCharArray(). toCharArray() simply does a System.arraycopy() of the backing array.

Related

String's CharAt method using long

I want to find the character at a particular position of a very large string. However i am unable to use charAt() method because the range exceeds that of int. Is there a tweak to this?
In Java, Strings are backed by a character array. The theoretical size of an array is limited by the maximum value of int, so it's impossible to have a String with over 231-1 characters to begin with.
To overcome this issue you can create a string class of your own that uses multiple arrays or strings as storage.
Would taking just a shorter substring from the large string and accessing the corresponding position help?
As the String is internally represented by array of chars its maximal length cannot be bigger than size of int. So in the first place you cannot have String that exceeds range of int.

Effective String Splitting

I have a 'Text' File from which I have to read data row-by-row. File contains around 1330 Rows. I need to read each row (which is a String) and then split it into substrings which will be inserted as data into database.
I'm able to read the data from the file row-by-row.
I'm able to insert data into database as well.
The Length of the String that I have to split has approximately 2750 characters. 1 option of splitting this String will be using 'substring(start, end)' method. However, as the line has 2750 characters, the number of splitted strings would be huge around 200 - 225 (I have mapping which suggests certain character length will have what string in Xml).
Can someone suggest any other technique of splitting these strings?
I suspect that given your numbers, your initial approach would be well within any standard JVM memory constraints.
As ever, premature optimisation is the root of all evil. I would try a simple split, and look to refine it if you have issues. I suspect at 200 strings over a line of 2700 chars that you won't have problems.
Note that the String object implements a flyweight pattern. That is, substring() doesn't replicate strings but merely reports back on a window on the original String's data (char array). Consequently an implementation using substring() will use very little extra memory (for what it's worth)
Since you already have the start/end defined and don't seem to even need to parse the string, the substring call is probably the fastest way. The lookups in substring will be hitting array indexes, addresses in memory, so the lookup is probably O(1)... and then maybe Java will copy out the particular string needed, but that's going to have to happen anyway and will only be O(n) even for all substrings if there's no overlap.
substring doesn't actually change the underlying string, it's just going to copy out the relevant portion you're looking for on each call (if it even does that, it would be theoretically possible for it to return a kind of String that encapsulated the original string). Unless you have identified an actual performance problem, the simplest solution is the best one.
If you had to split on, for example, commas, I'd use a CSVReader library.
you can use split() method of String class to split the string but for that string to be split it has to have some delimiter like comma, dash or something, and using that delimiter you can split the string.
String str = "one-two-three";
String[] temp;
/* delimiter */
String delimiter = "-";
/* given string will be split by the argument delimiter provided. */
temp = str.split(delimiter);

Android - Clean char array quickly and effectively

Is it possible to clean a char array in Java (Android) quickly and effectively? Make an other loop on the treatment seem to be to much heavy to be an optimised solution, doesn't it ?
It's strange but it is impossible to find such a solution in Java on the internet without having to subscribe to a pay site ..
Thank's to your attention
You can use:
Arrays.fill(yourArray, ' ');// or any other char instead of ' '
Arrays class belongs to the java.util package. It uses generics so you can fill any kind of array.
Why do you need to clear a char array in the first place?
Most functions that want a char array to put some data in it (IO read, etc..) return the length of data that was put in, so you can disregard the rest of the data in char array.
Use a boolean to indicate that your array is invalid.
I would like to know whether predetermine the size of an array which will be completed by various data sizes, or let it size evolve according to the reception is the best way? This is intended to have the fastest processing.
Thank's
I think the correct answer to this question is to set each character of the char[] to null character by doing:
Arrays.fill(buffer, (char)0);
Or
Arrays.fill(buffer, '\0');
Doing Arrays.fill(buffer, '0'); will fill all the char[] with the character '0' instead of the null character.

Help converting input string into Unicode integers in Java

I've been set an assignment which requires me to capture the input of 5 strings, convert them to uppercase, output them as a string, convert them to their Unicode integers (using the getNumericValue method) and then manipulate the integers using some basic operators.
I get the first part but I am having trouble with the following:
Using the getNumericValue to convert
my single character literal strings
into their Unicode integer
counterparts.
Being able to assign these ints to
variables so I can further process s
them with operators, all the
examples I have seen have been
simple printing out the number and
not assigning it to a variable,
since I am a Java noob the syntax is
still a little confusing for me.
My code is here
If there is a cleaner way of doing what I want please suggest so but without the use of arrays or loops.
I don't understand why you don't want to do this without arrays or loops.
You can get the unicode values (as ints) making up the string via String.codePointAt(), or get the characters via charAt() followed by a getNumericValue() for each character. But regardless, you're going to have to iterate over the set of characters in the string via a loop, or perhaps recursion.
Yes, sounds like op needs to continue researching avenues of learning.
// difficult to code without interfering with
// instructor's prerogative
char ( array ) = String.getchars();
// Now what, unroll the loop?
...
As noted by Brian, loops are fundamental. I cannot imagine getChars being assigned before simple array techniques.

Explanation of this line of code?

Can you explain what is happening in this line of code? Specially what is args[0].tocharArray ?
char[] password = args[0].toCharArray();
char[] is your datatype. "char" is a single 16 bit character, and char[] is a character array.
args[0] is the first argument that's passed to the program.
.toCharArray(); converts that argument to a character array.
This line of code is basically taking an argument, turning it into a character array, and storing it in "password" which is a character array.
It's converting the first argument of a Java program—passed as a String[] to the main method—to a character array.
Most password-oriented APIs use char[] so that after calling the method, the caller can "zero-ize" the array, effectively erasing the password from memory. Since Java String instances are immutable, they can't be zero-ized. However, in practice, it's hard to get user-input without using a String. All web frameworks will convert passwords submitted in a web request to a String. Swing password widgets and Java 6's Console class will input char[], however.
args[0] is presumably a String array. Thus it is a call to the method String.toCharArray() which converts a String to an array of chars.
EDIT: Corrected my answer after comment.
It converts the first item of the args array (presumably, the first command line argument passed to the main method, which is of string type) to an equivalent array of chars (an array containing all the chars that build up the string).
args is an array.
The type of the array contains a function called toCharArray which returns an array of characters. NOTE: args is most likely an array of strings
So it takes the string in args[0] and creates an array of characters which represents that string.
args[0] - representing a string
toCharArray() - convert this string to char array
I thought this, toCharArray(), might help.

Categories