码迷,mamicode.com
首页 > 编程语言 > 详细

Java面试题

时间:2018-05-14 23:06:58      阅读:356      评论:0      收藏:0      [点我收藏+]

标签:reverse   程序   todo   value   cond   bcd   length   字符串翻转   port   

 

0.写一个函数,找出字符串中关键字‘_‘出现所有的位置(如"acd124_145po_c"中‘_‘出现的位置是6和12)

 

 1 package cn.union;
 2 
 3 import java.util.ArrayList;
 4 import java.util.List;
 5 
 6 public class Union4 {
 7 
 8     public static void main(String[] args) {
 9         // TODO Auto-generated method stub
10 
11         String str = "acd124_145po_c";
12 
13         System.out.println(find_(str));
14     }
15 
16     private static List<Integer> find_(String str) {
17         // TODO Auto-generated method stub
18 
19         List<Integer> result = new ArrayList<Integer>();// list存放结果
20         char[] charArr = str.toCharArray();// 将字符串转为字符数组
21 
22         for (int i = 0; i < charArr.length; i++) {
23             if (‘_‘ == charArr[i]) {
24                 result.add(i);// 如果有_,则添加到list
25             }
26         }
27 
28         return result;
29     }
30 }

 

1.写一个函数,找出整形数组a中,值第二大的元素(只用一层循环)

 

 1 package cn.union;
 2 
 3 public class Union5 {
 4 
 5     public static void main(String[] args) {
 6         // TODO Auto-generated method stub
 7 
 8         int[] a = { 10, 1, 9, 2, 8, 3, 7, 4, 6, 5 };
 9 
10         System.out.println(findSecondMax(a));
11     }
12 
13     private static int findSecondMax(int[] a) {
14         // TODO Auto-generated method stub
15 
16         if (null == a || a.length <= 1) {
17             return -1;
18         }
19 
20         int max = a[0] > a[1] ? a[0] : a[1];
21         int secondMax = a[0] + a[1] - max;
22 
23         for (int i = 2; i < a.length; i++) {
24             if (a[i] > max) {
25                 secondMax = max;
26                 max = a[i];
27             } else if (a[i] > secondMax) {
28                 secondMax = a[i];
29             }
30         }
31 
32         return secondMax;
33     }
34 }

 

2.运行以上程序,输出结果是:

-128

因为byte最大值是127,最小值是-128

 

 1 package cn.union;
 2 
 3 public class Test {
 4 
 5     public static void main(String[] args) {
 6         // TODO Auto-generated method stub
 7 
 8         byte i = Byte.MAX_VALUE;
 9         byte j = ++i;
10         System.out.println(j);
11     }
12 }

 

3.写一个方法,将一个字符串翻转(如将字符串"abcdef"转成"fedcba")

 

 1 package cn.union;
 2 
 3 public class Union7 {
 4 
 5     public static void main(String[] args) {
 6         // TODO Auto-generated method stub
 7 
 8         String str = "abcdef";
 9 
10         System.out.println(myReverse(str));
11     }
12 
13     private static String myReverse(String str) {
14         // TODO Auto-generated method stub
15 
16         char[] charStr = str.toCharArray();
17         char[] result = new char[charStr.length];
18         int flag = 0;
19 
20         for (int i = charStr.length - 1; i >= 0; i--) {
21             result[flag] = charStr[i];
22             flag++;
23         }
24 
25         return String.valueOf(result);
26     }
27 }

 

Java面试题

标签:reverse   程序   todo   value   cond   bcd   length   字符串翻转   port   

原文地址:https://www.cnblogs.com/denggelin/p/9038448.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!