package cn.itcast.map;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
public class StringReplace {
//移除一段字符串中的下划线
public String remove(String resource, char ch){
StringBuffer buffer = new StringBuffer();
int position = 0;
char currentChar;
while(position < resource.length()){
currentChar = resource.charAt(position++);
if(currentChar != ch){
buffer.append(currentChar);
}
}
return buffer.toString();
}
//检测是否是回文字符串
public boolean CheckString(String resource){
int len = resource.length();
int begin = 0;
int end = len - 1;
while(begin != end){
if(resource.charAt(begin) == resource.charAt(end)){
begin++;
end--;
}
else{
return false;
}
}
return true;
}
//返回map集合中的最大值,并返回其对应的key
public Map<String, Integer> MaxMapValue(Map<Integer, Integer> map){
Integer maxValue = new Integer(0);
Integer maxKey = new Integer(0);
Map<String, Integer> mapResult = new HashMap<String, Integer>();
Set<Integer> sets = map.keySet();
for(Integer set:sets){
if(map.get(set).intValue() > maxValue.intValue())
{
maxValue = map.get(set);
maxKey = set;
}
}
mapResult.put("maxKey", maxKey);
mapResult.put("maxValue", maxValue);
return mapResult;
}
public static void main(String[] args){
String str = "ab_cd_12_34";
char cht = '_';
Map<Integer, Integer> map = new HashMap<Integer, Integer>();
map.put(1, 8);
map.put(3, 12);
map.put(5, 53);
map.put(123, 33);
map.put(42, 11);
map.put(44, 42);
map.put(15, 3);
String result = "";
boolean isornot0 = true;
boolean isornot1 = true;
StringReplace SRObject = new StringReplace();
result = SRObject.remove(str, cht);
System.out.println(result);
isornot0 = SRObject.CheckString("12345321");
isornot1 = SRObject.CheckString("123454321");
System.out.println(isornot0);
System.out.println(isornot1);
System.out.println(SRObject.MaxMapValue(map));
}
}
运行结果:
package cn.itcast.map;
public class Bisection {
private static int BisectionFind(int[] a,int tag){
int first = 0;
int end = a.length;
for(int i=0; i<a.length; i++){
int middle = (first + end)/2;
if(tag == a[middle]){
return middle;
}
if(tag > a[middle]){
first = middle + 1;
}
if(tag < a[middle]){
end = middle - 1;
}
}
return 0;
}
public static void main(String args[]){
int[] a={2,6,15,27,38,45,78,96};
int b = BisectionFind(a,27);
System.out.println(b);
}
}
运行结果:
package cn.itcast.map;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public class BubbleSort {
public static int[] Sort(int[] a){
int length = a.length;
for(int i=1; i<length; i++){
for(int j=0; j<length-1; j++){
if(a[j] <= a[j+1]){
int temp = a[j];
a[j] = a[j+1];
a[j+1] = temp;
}
}
}
return a;
}
public static int[] SortGood(int[] a){
int length = a.length;
for(int i=0; i<length; i++){
for(int j=i+1; j<length; j++){
if(a[i] <= a[j]){
int temp = a[i];
a[i] = a[j];
a[j] = temp;
}
}
}
return a;
}
public static List<Integer> arrayData(List<Integer> inputData){
for(int i=0;i<inputData.size();i++){
for(int j=i+1;j<inputData.size();j++){
if((int)inputData.get(i)<(int)inputData.get(j)){
int tem = (int)inputData.get(i);
inputData.set(i, inputData.get(j));
inputData.set(j, tem);
}
}
}
return inputData;
}
//泛型的优点:将运行阶段的问题拿到编译阶段处理、不需要强转、提高安全性(泛型在集合中应用较多)
public static List<Integer> arrayDataGeneric(List<Integer> inputData){
for(int i=0;i<inputData.size();i++){
for(int j=i+1;j<inputData.size();j++){
if((int)inputData.get(i)<(int)inputData.get(j)){
int tem = (int)inputData.get(i);
inputData.set(i, inputData.get(j));
inputData.set(j, tem);
}
}
}
return inputData;
}
public static void main(String[] args) {
int[] a = {4,5,24,13,16,32};
int[] a1 = {4,5,24,13,16,32};
List<Integer> b = new ArrayList<Integer>(Arrays.asList(4,5,24,13,16,32));
Sort(a);
SortGood(a1);
System.out.println("[" + a[0] + ", " + a[1] + ", " + a[2] + ", " + a[3] + ", " + a[4] + ", " + a[5] + "]");
System.out.println("[" + a1[0] + ", " + a1[1] + ", " + a1[2] + ", " + a1[3] + ", " + a1[4] + ", " + a1[5] + "]");
System.out.println(arrayData(b).toString());
}
}
运行结果:
package cn.itcast.map;
public class ReverseString {
/**
*功能:利用String.toCharArray()方法,将String转换为一个char型的数组,然后用
*数组遍历的方式从后往前遍历。
*/
String Reverse(String s){
char ch[] = s.toCharArray();
int start = 0, end = s.length() - 1;
//char是原生类型,2字节,无符号整型。而String是复杂对象,里面内涵了一个char[]数组,
//""空字符串,只是这个数组的长度=0,也就是0个char,而你是1个char,所以必须指定内容
char temp = '0';
while(start < end){
temp = ch[start];
ch[start] = ch[end];
ch[end] = temp;
start++;
end--;
}
String results = String.copyValueOf(ch);
return results;
}
/**
*通过jdk自带的reverse的方法
*/
void ReverseJDK(String s){
StringBuilder results = new StringBuilder(s);
System.out.println(results.reverse().toString());
}
/**
*使用String的charAt方法
*/
String ReverseCharAt(String s){
int length = s.length();
String reverse = "";
for(int i = 0; i < length; i++){
//拼接
reverse = s.charAt(i) + reverse;
}
System.out.println(reverse);
return reverse;
}
public static void main(String args[]){
String s = "abcd";
ReverseString rs = new ReverseString();
System.out.println(rs.Reverse(s));
rs.ReverseJDK(s);
rs.ReverseCharAt(s);
}
}
运行结果: