package cn.itcast.api.String.test;

 public class StringTest_1 {

     public static void main(String[] args) {

         String s1 = "asdfitcastghijfghjk";
String s2 = "xcitcastvbnm"; String maxSubStirng = getMaxSubstring(s2, s1);
System.out.println("maxSubString:" + maxSubStirng);
/*
* 作业1:获取两个字符串的最大相同子串。 "asdfitcastghijfghjk" "xcitcastvbnm"
*
* 思路: 1,先明确两个字符串的长短,在长串中判断短串是否存在。 2,存在,已找到,说明短串就是最大的相同。
* 不存在,就将短串按照长度递减的方式,获取短串中的子串,并到长串中判断。 3,一旦存在,便结束查找。
*/ } private static String getMaxSubstring(String s1, String s2) {
String max, min;
// 明确哪个是长串,哪个是短串。
max = (s1.length() > s2.length()) ? s1 : s2;
min = max.equals(s1) ? s2 : s1; // 验证max和min.
/*
* System.out.println("max:"+max); System.out.println("min:"+min);
*/ for (int i = 0; i < min.length(); i++) {
for (int start = 0, end = min.length() - i; end < min.length(); start++, end++) {
String temp = min.substring(start,end);
if(max.contains(temp)){
return temp;
}
}
}
return null;
} }
 package cn.itcast.api.String.test;

 import java.util.Arrays;

 public class StringTest_2 {

     public static void main(String[] args) {
String str = "cfdasbv";
str = sortStringByChars(str);
System.out.println("str="+str); }
/*
*
* 作业2:对字符串中字符进行自然顺序排序。
* cfdasbv--->abcdfsv
*
* 思路:
* 1,排序都是对数组排序。
* 2,数组中的元素都是在字符串中,把字符串转成数组。
* 3,对数组排序。
* 4,将排序后的数组转成字符串。
*/ public static String sortStringByChars(String str) {
//1,将字符串转成数组。转成字符数组。
char[] chs = getArray(str);
//2,对数组排序。
sort(chs);
//3,将排序后的数组转成字符串。返回。
return new String(chs);
} private static void sort(char[] chs) {
Arrays.sort(chs); } /**
* 将字符串转成字符数组。
* @param str
* @return
*/
private static char[] getArray(String str) {
// TODO Auto-generated method stub
return str.toCharArray();
} }
 package cn.itcast.api.String.test;

 public class StringTest_3 {
public static void main(String[] args){
String str = " it cast ";
// String s1 = str.trim();
String s1 = myTrim(str);
System.out.println("-"+s1+"-");
} /**
* 模拟trim功能。
*
*/
public static String myTrim(String str){ //定义两个变量,一个记录头,一个记录尾。
int start = 0;
int end =str.length()-1; //2,获取头部非空白的位置,
while(start<=end && str.charAt(start)==' '){
start++;
}
//2,获取尾部非空白的位置,
while(start<=end && str.charAt(end)==' '){
end--;
} //截取字符串。
return str.substring(start, end+1);
}
}
 package cn.itcast.api.wrapper.demo;

 public class WrapperDemo {

     public static void main(String[] args) {
/*
* 场景:通过文本框获取用户输入的数字数据,可是得到的都是字符串。
* 如果想要对字符串中的数字进行运算,必须要将字符串转成数字。
* JAVA提供了相应的解决对象。
* 基本数据类型对象包装类:Java将基本数据类型值封装成对象。
* 封装成对象有什么好处?因为可以提供更多的操作基本数值的方法。
*
* byte Byte
* short Short
* int Integer
* long Long
* float Float
* double Double
* boolean Boolean
* char Character
*/ //学习一下整数Integer
/*
* 基本类型对象包装类特点:
* 1,用于在基本数据类型和字符串之间进行转换。
* int parseInt(String);
* byte parseByte(String);
* boolean parseBoolean(String);
*
* xxx parseXxx(String);
* 只有Character没哟解析方法。
*
*
*/ /*System.out.println(Integer.MAX_VALUE);
System.out.println(Integer.toBinaryString(10));
System.out.println(Integer.toBinaryString(-6));*/ //1,字符串--->基本数值。结果:基本数值(字符串)演示Integer int(String) System.out.println(Integer.parseInt("123")+2);//NumberFormaExctpioon
System.out.println(Integer.parseInt("2222", 10));//按照指定的进制进行转换。 //2,基本数值转成字符串。34+"" String.valueOf(34)Integer.toString(int);
System.out.println(34+5); //3,基本数值---》包装对象。
Integer i = new Integer(4);
Integer ii = Integer.valueOf("4"); //包装对象---》基本数据类型
int num = i.intValue();
System.out.println(num);
} }
 package cn.itcast.api.wrapper.demo;

 public class WrapperDemo2 {

     public static void main(String[] args) {

 //        int i = 4;
// Integer i = new Integer(4);
// JDK1.5以后,有了一个包装类的新特性。目的简化书写。自动装箱
Integer i = 4;//自动装箱。Integer.valueOf(4);
i = i+5;//原理是:等号右边将i对象转成基本数值。i.intValue()+5;加法运算后,在此装箱。
//i = Integer.valueOf(i.inValue()+5);
System.out.println(i); Integer a = new Integer(3);
Integer b = new Integer(3);
System.out.println(a==b);
System.out.println(a.equals(b)); System.out.println("===============");
Integer x = 127;
Integer y = 127;
System.out.println(x==y);
System.out.println(x.equals(y)); } }
 //WrapperTest.java
package cn.itcast.api.wrapper.demo; import java.util.Arrays; public class WrapperTest { public static void main(String[] args) {
/*
* 练习:面试题:
* "23 9 -4 18 100 7";
* 要求对这串数字按照从大到小排序,生成一个数值有序的字符串。
*/
String numString = "23 9 -4 18 100 7";
numString = sortNumberString(numString);
System.out.println("nums="+numString); } public static String sortNumberString(String numString) {
//1一个字符串通过分割变成多个字符串。split();
String[] strs = numString.split(" "); //2,不能直接对字符串大小排序。因为字符串23比字符串9要小。必须转成整数值。
//将字符串数组转成int[] 数组。
int[] nums = parseIntArray(strs); //3,对数组排序。
Arrays.sort(nums); // 4,将数组转成字符串。
return toString(nums);
}
private static String toString(int[] nums) {
StringBuilder sb = new StringBuilder();
for (int i = 0; i < nums.length; i++) {
if(i!=nums.length-1){
sb.append(nums[i]+" ");
}else{
sb.append(nums[i]);
}
}
return sb.toString();
} //将字符串数组转成int[] 数组
public static int[] parseIntArray(String[] strs) {
//定义一个int数组。
int[] arr = new int[strs.length]; //2,遍历字符串数组。把元素转成int存储到int数组中。
for (int i = 0; i < strs.length; i++) {
arr[i] = Integer.parseInt(strs[i]);
}
return arr;
} }

《day18_String练习_基本类型包装类_集合入门》的更多相关文章

  1. 简单物联网:外网访问内网路由器下树莓派Flask服务器

    最近做一个小东西,大概过程就是想在教室,宿舍控制实验室的一些设备. 已经在树莓上搭了一个轻量的flask服务器,在实验室的路由器下,任何设备都是可以访问的:但是有一些限制条件,比如我想在宿舍控制我种花 ...

  2. 利用ssh反向代理以及autossh实现从外网连接内网服务器

    前言 最近遇到这样一个问题,我在实验室架设了一台服务器,给师弟或者小伙伴练习Linux用,然后平时在实验室这边直接连接是没有问题的,都是内网嘛.但是回到宿舍问题出来了,使用校园网的童鞋还是能连接上,使 ...

  3. 外网访问内网Docker容器

    外网访问内网Docker容器 本地安装了Docker容器,只能在局域网内访问,怎样从外网也能访问本地Docker容器? 本文将介绍具体的实现步骤. 1. 准备工作 1.1 安装并启动Docker容器 ...

  4. 外网访问内网SpringBoot

    外网访问内网SpringBoot 本地安装了SpringBoot,只能在局域网内访问,怎样从外网也能访问本地SpringBoot? 本文将介绍具体的实现步骤. 1. 准备工作 1.1 安装Java 1 ...

  5. 外网访问内网Elasticsearch WEB

    外网访问内网Elasticsearch WEB 本地安装了Elasticsearch,只能在局域网内访问其WEB,怎样从外网也能访问本地Elasticsearch? 本文将介绍具体的实现步骤. 1. ...

  6. 怎样从外网访问内网Rails

    外网访问内网Rails 本地安装了Rails,只能在局域网内访问,怎样从外网也能访问本地Rails? 本文将介绍具体的实现步骤. 1. 准备工作 1.1 安装并启动Rails 默认安装的Rails端口 ...

  7. 怎样从外网访问内网Memcached数据库

    外网访问内网Memcached数据库 本地安装了Memcached数据库,只能在局域网内访问,怎样从外网也能访问本地Memcached数据库? 本文将介绍具体的实现步骤. 1. 准备工作 1.1 安装 ...

  8. 怎样从外网访问内网CouchDB数据库

    外网访问内网CouchDB数据库 本地安装了CouchDB数据库,只能在局域网内访问,怎样从外网也能访问本地CouchDB数据库? 本文将介绍具体的实现步骤. 1. 准备工作 1.1 安装并启动Cou ...

  9. 怎样从外网访问内网DB2数据库

    外网访问内网DB2数据库 本地安装了DB2数据库,只能在局域网内访问,怎样从外网也能访问本地DB2数据库? 本文将介绍具体的实现步骤. 1. 准备工作 1.1 安装并启动DB2数据库 默认安装的DB2 ...

  10. 怎样从外网访问内网OpenLDAP数据库

    外网访问内网OpenLDAP数据库 本地安装了OpenLDAP数据库,只能在局域网内访问,怎样从外网也能访问本地OpenLDAP数据库? 本文将介绍具体的实现步骤. 1. 准备工作 1.1 安装并启动 ...

随机推荐

  1. 联想G480笔记本安装系统

    联想G480笔记本安装系统 联想G480笔记本,配置i5双核四线程处理器,4G内存,500G硬盘,USB3.0接口,NVIDIA GeForce GT 610M+Intel HD Graphics 3 ...

  2. Apache Commons fileUpload实现文件上传之一

      需要两个jar包: commons-fileupload.jar Commons IO的jar包(本文使用commons-io-2.4.jar) 利用Servlet来实现文件上传. package ...

  3. robotframework笔记6

    测试文件结构 *** Settings *** Library OperatingSystem Library BuiltIn Resource ressources.py *** Variables ...

  4. Python安装BeautifulSoup库(Windows平台下)

    简介 参照官网Beautiful Soup4.4.0文档:https://www.crummy.com/software/BeautifulSoup/bs4/doc/ 安装步骤 1.到https:// ...

  5. python中文处理

    源码文件为utf-8格式  CODEC = 'utf-8': VS在“高级保存选项”中选择“UTF-8 65001” input(u'中文');print(u'中文')

  6. input固定定位后,当input框获取到焦点时,会离开手机软键盘的解决方法

    前些天做页面时候遇到这样一个问题,将input框用position:fixed固定定位在最底部的时候,当Input获取焦点的时候,input框离开了手机软键盘,而不是吸附在软键盘上,效果如下图: 找了 ...

  7. MVC1-5直接访问静态页面

    MVC模式下默认是无法访问站点内静态页面,昨日百度找了半天试了半天才试成功. 默认在Views文件外的静态页面可以访问,若要访问Views里的静态页面则需要修改View文件夹中的web.config: ...

  8. hibernate generator class=xxx id详解

    <id>元素中的<generator>用来为该持久化类的实例生成唯一的标识,hibernate提供了很多内置的实现.Increment:由hibernate自动递增生成标识符, ...

  9. office 使用技巧

    Excel: 非打印区域显示成灰色:视图--分页预览 选定行的时侯,如何从某一行选定到末尾? 先点某一行,然后按住shift不松,再按END,再按下方向箭. 选定列也是这样,先按住某列,然后按SHIF ...

  10. 线程系列4---sleep()和wait()方法区别

    2013-12-25 14:49:00 1. sleep()方法是Thread类的一个静态方法,可以在任意地方被调用,而wait()方法是object类的一个方法,只能在同步代码块或者同步方法里面,通 ...