中软笔试,不用中间变量字符串倒转,10000个数求第2大的数,不许用排序算法. .Java中的属性和字段有什么区别?

1.不用中间变量字符串倒转

String str="ABCD";
for (int i = str.Length-1; i >=0; i--)
str+=str[i];
str=str.Substring(str.Length/2, str.Length/2);
Console.WriteLine(str);
Console.ReadKey();

2.10000个数求第2大的数,不许用排序算法.

class T {
public static void main(String[] args) {
System.out.println(getSecondMax(new int[] { 1, 3, 5, 7, 9, 2, 4, 6, 8, 10 }));
}
/**
* 求整数数组的第2大的数,不许用排序算法
*
* @param nums
* @return
*/
public static int getSecondMax(int[] all) {
int max, max2;// 第一大,第二大数字
max = all[0];
max2 = all[1];
if (max < max2) {
max = all[1];
max2 = all[0];
}
for (int i = 1; i < all.length; i++) {
if (all[i] > max2) {
if (all[i] > max) {
max2 = max;// 原来最大值变第二大
max = all[i];// 最大值更新为当前值
} else
max2 = all[i];// 当前值为第二大
}
}
return max2;
}
}

http://topic.csdn.net/u/20100309/21/95f23e3f-4bd3-4cf0-a205-a2bc50a1c74e.html

3.Java中的属性和字段有什么区别? 答:Java中的属性,通常可以理解为get和set方法。而字段,通常叫做“类成员”。

这两个概念是完全不同的。

属性只局限于类中方法的声明,并不与类中其他成员相关。例如: void setA(String s){} String getA(){} 当一个类中拥有这样一对方法时,我们可以说,这个类中拥有一个可读写的a属性(注意是小写a)。如果去掉了set的方法,则是可读属性,反之亦然。

类成员(字段),通常是在类中定义的类成员变量,例如: public class A{ private String s = "123"; } 我们可以说A类中有一个成员变量叫做s。