The bottom layer of ArrayList is realized by an object array with the length of 10. No matter what type of data is added, it will be converted into an object unless you used the JDK version a long time ago. This is easy to understand. For example, the program arrayList 1 you wrote adds two types of data: string and integer. These two types of data are incomparable, just like comparing a lighter with a USB flash drive. So there is no way to sort directly.
What you need is to sort by 1, 2,4 data in ArrayList. This is possible.
The first is ArrayList 1
The four data are 2, "daas", 6, "1", 1, and the data of 2 and 4 are 2, "daas" and "1" respectively. I choose to sort by string type, so 1 data2 can be converted to string type. Because the third data 6 is not sorted, removal is good. This is a question or something, but it actually leaves some room, because the following codes (under your arrayList2\3\4) are all added to arrayList 1, and they are all string types. That's why I choose the string type for sorting. The code is as follows:
list ArrayList 1 = new ArrayList();
ArrayList 1 . add(2); //0
ArrayList 1 . add(" daas "); // 1
ArrayList 1 . add(6); //2
ArrayList 1 . add(" 1 "); //3
list . add(ArrayList 1);
//My code
string convert = string . value of(ArrayList 1 . get(0));
ArrayList 1 . remove(2);
ArrayList 1 . remove(0);
ArrayList 1 . add(convert);
?
/Is this your arrayList2\3\4 code?
?
collections . sort(ArrayList 1);
for(int I = 0; I< ArrayList1.size (); i++) {
system . out . println(ArrayList 1 . get(I));
}
The results are as follows:
1
2
three
five
eight
daas
Because I am afraid that you will dig deep, emphasize two points:
First of all, from the results, we can see that collections.sort (Arraylist1) is sorted by the ASCII code of the string. To prove this, we need to look at the original code. At this time, you will find that the compareTo method of JDK's String is an empty implementation, and the underlying layer is not written in java. It doesn't matter. We can expose its characteristics in a certain way, and then we can understand the idea. Just look at the small test program I wrote below.
String a = new string ("z");
String b = new string ("a");
system . out . println(a . compare to(b));
String c = new string ("a");
String d = new string ("b");
system . out . println(c . compare to(d));
//Look at the results and prove that the comparison of natural sequences of String, that is, the comparison of ASCII values, is only the first step.
//Look at the results, and prove that the return value of compareTo is the later ASCII code minus the previous ASCII code. Step two.
String e = new string ("g");
String f = new string ("e");
String h = new string ("h");
List & lt string & gtlist = new ArrayList & lt string & gt ();
list . add(e);
list . add(f);
list . add(h);
collections . sort(list);
for(String i : list) {
system . out . println(I);
}
The results are as follows:
25
- 1
e
g
h
//The last step is to prove the natural sorting of String, that is, ASCII codes are sorted from small to large.
Second, your request is "how to sort according to 1 and 2.4 data in ArrayList". The description of this problem is problematic or not detailed. In ArrayList 2, arrayList3, arrayList4, three data are added as soon as they are * * *, so there is no fourth data. If the data in arrayList2\3\4 are sorted separately, two choices, (1), will convert all integers into string types, and then sort them. Refer to arrayList2. (2) Using Integer.valueOf () method, the string data containing numerical content is converted into integers, and the original string data containing non-numerical content is removed, and then sorting, bucket sorting, bubble sorting, quick sorting, etc.
At this point, no matter which list you want to sort, you should have written it.
Summary:
Unless you compare ASCII codes, there is no way to compare integer and string data with non-numerical content, whether directly or indirectly. Again, just like lighters and USB flash drives are not comparable, it is important to understand this.
That is, it is useless to use the Integer.valueOf () method to convert string data of non-numeric content, and it will be reported to NumberFormatException. This means that if you want to sort by integer type, you must delete all string data whose contents are not numbers.
Off-topic: I remember this kind of problem when I was in college. I don't know if you are a student. It's really useless to think about it today. It's all generic. Programmers who don't use generics to write code now almost don't exist.
Have a good mood ~ ~
Fight with your own hands. Give it to me if you are satisfied ~ ~ haha. .