0. Write a function to find out where the keyword ' _ ' in the string appears all the places (such as ' _ ' in "Acd124_145po_c" where the position is 6 and 12)
1 Packagecn.union;2 3 Importjava.util.ArrayList;4 Importjava.util.List;5 6 Public classUnion4 {7 8 Public Static voidMain (string[] args) {9 //TODO auto-generated Method StubTen OneString str = "Acd124_145po_c"; A - System.out.println (Find_ (str)); - } the - Private StaticList<integer>find_ (String str) { - //TODO auto-generated Method Stub - +list<integer> result =NewArraylist<integer> ();//List Storage Results - Char[] Chararr = Str.tochararray ();//converting a string to a character array + A for(inti = 0; i < chararr.length; i++) { at if(' _ ' = =Chararr[i]) { -Result.add (i);//If you have _, add to the list - } - } - - returnresult; in } -}
1. Write a function to find the second largest element in the shape array a (with only one layer of loops)
1 Packagecn.union;2 3 Public classUnion5 {4 5 Public Static voidMain (string[] args) {6 //TODO auto-generated Method Stub7 8 int[] A = {10, 1, 9, 2, 8, 3, 7, 4, 6, 5 };9 Ten System.out.println (Findsecondmax (a)); One } A - Private Static intFindsecondmax (int[] a) { - //TODO auto-generated Method Stub the - if(NULL= = A | | A.length <= 1) { - return-1; - } + - intmax = a[0] > a[1]? A[0]: a[1]; + intSecondmax = a[0] + a[1]-Max; A at for(inti = 2; i < a.length; i++) { - if(A[i] >max) { -Secondmax =Max; -Max =A[i]; -}Else if(A[i] >Secondmax) { -Secondmax =A[i]; in } - } to + returnSecondmax; - } the}
2. Run the above program, the output is:
-128
Because the maximum byte value is 127, the minimum value is-128
1 Packagecn.union;2 3 Public classTest {4 5 Public Static voidMain (string[] args) {6 //TODO auto-generated Method Stub7 8 bytei =Byte.max_value;9 bytej = + +i;Ten System.out.println (j); One } A}
3. Write a method to flip a string (such as the string "abcdef" to "FEDCBA")
1 Packagecn.union;2 3 Public classUnion7 {4 5 Public Static voidMain (string[] args) {6 //TODO auto-generated Method Stub7 8String str = "ABCdef";9 Ten System.out.println (Myreverse (str)); One } A - Private Staticstring Myreverse (String str) { - //TODO auto-generated Method Stub the - Char[] Charstr =Str.tochararray (); - Char[] result =New Char[charstr.length]; - intFlag = 0; + - for(inti = charstr.length-1; I >= 0; i--) { +Result[flag] =Charstr[i]; Aflag++; at } - - returnstring.valueof (result); - } -}
Java face question