资源描述
1. 写一种函数,例如:给你 a b c 则输出 abc acb bac bca cab cba
package test;
import java.util.ArrayList;
import java.util.List;
public class ListOpreation {
public static void main(String[] args){
String s = "ABCD";
List<String> list = list(s,"");
System.out.println(list.size());
System.out.println(list);
}
public static List<String> list(String base,String buff){
List<String> result = new ArrayList<String>();
if(base.length()<=0){
result.add(buff);
System.out.println(result.toString()+"&");
}
for(int i=0;i<base.length();i++){
List<String> temp = list(new StringBuilder(base).deleteCharAt(i).toString(),buff+base.charAt(i));
result.addAll(temp);
}
for(int i=0;i<base.length();i++){
System.out.println(new StringBuilder(base).deleteCharAt(i).toString()+"=");
System.out.println(base.charAt(i)+"*");
System.out.println(buff+"#");
}
return result;
}
}
执行成果:
2. 写一种函数,给你一种字符串 倒序输出来。
package test;
public class ListOperation {
public static void main(String[] args){
String str = "aadsfdfgdfcvsdfsdf";
strOperation(str);
}
private static void strOperation(String str) {
// TODO Auto-generated method stub
System.out.println("str:"+str);
String newStr = "";
for(int i=0;i<str.length();i++){
char c = str.charAt(str.length()-1-i);
newStr = newStr + c;
}
System.out.println("newStr:"+newStr);
}
}
执行成果:
3. 不使用中间变量 把两个变量值互换
package test;
public class ListOperation1 {
public static void main(String[] args){
int a = 20;
int b = 30;
a = a*b;
b = a/b;
a = a/b;
System.out.println("a= "+a+'\n'+"b= "+b);
}
}
4. 冒泡排序
package test;
public class ListOperation2 {
public static void main(String[] args){
int[] list = {20,27,15,29,4,14,28};
sort(list);
for(int i=0;i<list.length;i++){
System.out.print(list[i]+" ");
}
System.out.println();
}
public static int[] sort(int[] list){
int temp;
for(int i=0;i<list.length;i++){
for(int j=i+1;j<list.length;j++){
if(list[i]<=list[j]){
temp = list[j];
list[j] = list[i];
list[i] = temp;
}
}
}
return list;
}
}
执行成果:
5. 将一种文献复制到另一种文献中。
package test;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
public class ListOperation3 {
public static void main(String[] args){
File oldFile = new File("D:\\1.txt");
File newFile = new File("D:\\2.txt");
try {
FileInputStream fis = new FileInputStream(oldFile);
FileOutputStream fos = new FileOutputStream(newFile);
int read = 0;
while((read=fis.read())!=-1){
fos.write(read);
fos.flush();
}
fos.close();
fis.close();
} catch (FileNotFoundException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
执行成果:
6. 将this is a test 转化为This Is A Test
package test;
public class ListOperation4 {
public static void main(String[] args){
String str = new String("this is a good person");
char[] c = new char[str.length()];
str.getChars(0,str.length(),c,0);
if(c[0]>='a'&&c[0]<='z'){
c[0] = (char)(c[0]-32);
}
for(int i=1;i<str.length();i++){
if(c[i]==' '){
c[i+1] = (char)(c[i+1]-32);
}
}
String str1 = new String(c);
System.out.println("Str1:"+str1);
}
}
执行成果:
7. 题目:输入一行字符,分别记录出其中英文字母、空格、数字和其他字符个数。
package test;
import java.util.Scanner;
public class ListOperation5 {
public static void main(String[] args){
int countNum = 0;
int countWord = 0;
int countBlank = 0;
int countOther = 0;
Scanner in = new Scanner(System.in);
System.out.println("请输入字符串:");
String str = in.nextLine();
char[] ch = str.toCharArray();
for(int i=0;i<str.length();i++){
if(ch[i]>='0'&&ch[i]<='9'){
countNum++;
}
else if((ch[i]>='a'&&ch[i]<='z')||(ch[i]>='A'&&ch[i]<='Z')){
countWord++;
}
else if(ch[i]==' '){
countBlank++;
}
else{
countOther++;
}
}
System.out.println("字符串中数字个数:"+countNum+'\n'
+"字符串中字母个数:"+countWord+'\n'
+"字符串中空格个数:"+countBlank+'\n'
+"字符串中其她字符个数:"+countOther+'\n');
}
}
执行成果:
展开阅读全文