Answer the question
In order to leave comments, you need to log in
What exactly does this line of code do?
This method seems to sort the array of strings by the number of the word in a single string.
public static void argumentSort(ArrayList<String> arr, int sortNumber) {
while (true) {
int j = 0;
for (int i = 0; i < arr.size() - 1; i++) {
String[] str1 = arr.get(i).split(" ");
String[] str2 = arr.get(i + 1).split(" ");
String st1 = arr.get(i);
String st2 = arr.get(i + 1);
char a = str1[sortNumber - 1].charAt(0);
char b = str2[sortNumber - 1].charAt(0);
if (a > b) {
arr.set(i + 1, st1);
arr.set(i, st2);
j++;
}
}
if (j == 0) break;
}
}
char a = str1[sortNumber - 1].charAt(0);
Answer the question
In order to leave comments, you need to log in
See str is an array of type String, i.e. for example, index 0 is a string (Masha), index 1 is a string (Petya), index 2 is a string (Vasya).
when an array is accessed by index str[1] - this is petya. str[2] is Bob, str[0] is Masha. Those. what's in square brackets is the position number in that array (starts from zero).
Those. we get str1[sortNumber - 1] which is the same as str[1] or 2 or 3, only this number is calculated in brackets.
sortNumber has its number in that context and minus 1 = the number to be enclosed in square brackets.
Move on. After these calculations, a function is added to this line, i.e. for example, we have an array str1 at position "x", where Masha, Vasya or Petya are located, the .charAt (0) function is used.
We go to Google and see what .charAt is - we get "charAt(int index) method returns the character at the specified index in a string
" charAt returns a "letter", not a word, but a char at a specific string index. charAt(0) - zero position in the string.
If we specifically take an example with Vasya(0), Masha(1), Petya(2), then
str1[2].charAt(0) is the first letter from Petya, that is, "p".
Explained as simply as possible, do not throw slippers :D
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question