把字符串中的单词拼写翻转,文中单词以英文空格分隔,整个句子单词顺序不变 This is a Java test
[output]:sihT a avaj tset
package test.interview;
/**
* 把字符串中的单词拼写翻转,文中单词以英文空格分隔,整个句子单词顺序不变 This is a Java test
* [output]:sihT a avaj tset
* **/
public class TestString {
public static void main(String[] args) {
String aa="This is a Java test";
StringBuffer dd=changeStr(aa);
System.out.println(dd);
}
public static StringBuffer changeStr(String aa){
String[] cc=aa.split(" ");
StringBuffer ee=new StringBuffer();
for (int i = 0; i < cc.length; i++) {
StringBuffer s=new StringBuffer(cc[i]);
if(i==(cc.length-1)){
ee.append(s.reverse());
}else{
ee.append(s.reverse()+" ");
}
}
return ee;
}
}
当调用new B()后,输出的内容是0,0
package test.interview;
/**
*A是父类,B是子类,B继承于A
*new B()的创建过程是 1.父类构造器;2.子类重写的父类方法;3.父类的原方法(子类未重写的方法);4.最后调用子类构造器
*
* **/
public class testAB {
public static void main(String[] args) {
B b=new B();
}
}
class A {
public A() {
System.out.println("调用父类构造器");
print();
}
public void print() {
System.out.println("调用父类的打印函数");
}
}
class B extends A {
int x = 1;
int y;
public B() {
System.out.println("调用子类构造器");
y= 1;
}
@Override
public void print() {
System.out.println("调用子类的打印函数");
System.out.println(x + "," + y);
}
}
已知类SubClass的getSum方法返回其父类成员i与类SubClass成员j的和;类SuperClass中的getSum为抽象函数,最后给出程序运行后的输出结果。
package test.interview;
/**
* 父类和子类都有不止一个构造器,执行顺序如何
* this的使用
* */
public class UainJava {
public static void main(String[] args) {
SuperClass s = new SubClass();
System.out.println(s.getValue());
System.out.println(s.getSum());
}
}
abstract class SuperClass {
private int i;
public SuperClass() {
System.out.println("父类构造器");
i = 5;
}
public int getValue() {
return i;
}
public abstract int getSum();
}
class SubClass extends SuperClass {
int j;
public SubClass() {
this(-3);
System.out.println("子类无参构造器");
}
public SubClass(int j) {
this.j = j;
System.out.println("子类有参构造器");
}
public int getValue() {
return j;
}
@Override
public int getSum() {
return this.getValue() + j;
}
}
一列数的规则如下:1,1,2,3,5,8,13,21,34.。。。求第n位数是多少,用Java代码实现
package test.interview;
/**
*
* 一列数的规则如下:1,1,2,3,5,8,13,21,34.。。。求第n位数是多少
* */
public class RecursionSum {
public static void main(String[] args) {
int sum = getSum(6);
System.out.println("递归求和 " + sum);
}
public static int getSum(int n) {
if (n == 1 || n == 2) {
return 1;
} else {
return getSum(n - 1) + getSum(n - 2);
}
}
}
用Java实现函数List distinct(List strValues),要求返回一个List,它的元素是参数strValues去掉重复项的结果。
package test.interview;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
/**
*实现函数List<String> distinct(List<String> strValues),要求返回一个List,
*它的元素是参数strValues去掉重复项的结果。
* **/
public class DistinctList {
public static void main(String[] args) {
List<String> strValues=new ArrayList<String>();
strValues.add("test1");
strValues.add("test1");
strValues.add("test2");
System.out.println(strValues);
List<String> newDisList=distinct(strValues);
System.out.println(newDisList);
}
public static List<String> distinct(List<String> strValues){
Set<String> disSet=new HashSet<String>();//定义HashSet
for (String str : strValues) {
disSet.add(str);
}
List<String> newDisList=new ArrayList<String>();
for (String string : disSet) {
newDisList.add(string);
}
return newDisList;
}
}
根据输入的字符串数组,找出数字(整型)并计算总和,并按顺序把非整形元素 转换成大写 累加到一起输出。
package test.interview;
import java.util.ArrayList;
import java.util.List;
/**
* 根据输入的字符串数组,找出数字(整型)并计算总和,并按顺序把非整形元素 转换成大写 累加到一起输出。
* */
public class Person {
public static void main(String[] args) {
String[] strs = { "this", "is", "12", "a", null, "test", "23" };
List<Integer> ints = new ArrayList<>();
int sum=0; //求和
StringBuffer s = new StringBuffer();//字符串拼接
for (String str : strs) {
Integer num = null;
try {
num = Integer.parseInt(str);
} catch (NumberFormatException e) {
// System.out.println("非数字");
if (str != null) {
s.append(str.toUpperCase() + " ");
}
}
if (num != null) {//去掉null
ints.add(num);
sum+=num;
}
}
System.out.println(ints);
System.out.println("result:"+sum);
System.out.println(s);
}
}
输出StringBuffer和反射代码输出结果
package test.interview;
import java.lang.reflect.Method;
import java.util.ArrayList;
/**
* StringBuffer和反射代码输出结果
* */
public class GetResult {
public static void main(String[] args) {
System.out.println("StringBuffer的处理");
StringBuffer sb=new StringBuffer("Hello");
System.out.println("Before:"+sb.toString());
changeStr(sb);
System.out.println("After:"+sb.toString());
System.out.println("反射测试");
ArrayList<String> a=new ArrayList<String>();
a.add("testReflect");
Class<? extends ArrayList> c=a.getClass();
try {
Method method=c.getMethod("add", Object.class);
method.invoke(a, 100);
System.out.println(a);
} catch (Exception e) {
// TODO: handle exception
}
}
public static void changeStr(StringBuffer sb){
sb=new StringBuffer("world");
}
}
package test.test;
/***
* String字符串操作
*
*/
public class test {
public static void main(String[] args) {
String s1 = "a";
String s2 = "aa";
System.out.println((s1 + s1) == s2); // s1 + s1在新地址生成字符串aa
System.out.println((s1 + s1).equals(s2));
String s3 = " hello ";// 前后各有一个空格
String s4 = " world ";// 前后各有一个空格
s3.trim();
s4.trim();
System.out.println(s3 + s4); // s3,s4 trim后没有被接收,所以s3,s4仍然是带空格的原字符串
}
}