JAVA实例
- 前言:
- 一.Java 字符串
-
- 1.Java 实例 – 字符串比较
- 2.Java 实例 - 查找字符串最后一次出现的位置
- 3.Java 实例 - 删除字符串中的一个字符
- 4.Java 实例 - 字符串替换
- 5.Java 实例 - 字符串反转
- 6.Java 实例 - 字符串查找
- 7.Java 实例 - 字符串分割
- 8.Java 实例 - 字符串分割(StringTokenizer)
- 9.Java 实例 - 字符串小写转大写
- 10.Java 实例 - 测试两个字符串区域是否相等
- 11.Java 实例 - 字符串性能比较测试
- 12.Java 实例 - 字符串优化
- 13.Java 实例 - 字符串格式化
- 14.Java 实例 - 连接字符串
- 二.Java 数组
-
- 1.Java 实例 – 数组排序及元素查找
- 2.Java 实例 – 数组添加元素
- 3.Java 实例 – 获取数组长度
- 4.Java 实例 – 数组反转
- 5.Java 实例 – 数组输出
- 6.Java 实例 – 数组获取最大和最小值
- 7.Java 实例 – 数组合并
- 8.Java 实例 – 数组填充
- 9.Java 实例 – 数组扩容
- 10.Java 实例 – 查找数组中的重复元素
- 11.Java 实例 – 删除数组元素
- 12.Java 实例 – 数组差集
- 13.Java 实例 – 数组交集
- 14.Java 实例 – 在数组中查找指定元素
- 15.Java 实例 – 判断数组是否相等
- 16.Java 实例 - 数组并集
- 三.Java 时间处理
前言:
因为菜鸟教程中的实例跳转,我个人觉得不是很方便,把实例放在一个页面上,方便我记录学习,所以产生此笔记,如果想看菜鸟教程的,直接点击链接就可以跳转:菜鸟教程
java菜鸟学习实例(一)
java菜鸟学习实例(二)
java菜鸟学习实例(三)
java菜鸟学习实例(四)
java菜鸟学习实例(完整版)
一.Java 字符串
1.Java 实例 – 字符串比较
public class StringCompareEmp{
public static void main(String args[]){
String str = "Hello World";
String anotherString = "hello world";
Object objStr = str;
System.out.println( str.compareTo(anotherString) );
System.out.println( str.compareToIgnoreCase(anotherString) ); //忽略大小写
System.out.println( str.compareTo(objStr.toString()));
}
}
2.Java 实例 - 查找字符串最后一次出现的位置
public class SearchlastString {
public static void main(String[] args) {
String strOrig = "Hello world ,Hello Runoob";
int lastIndex = strOrig.lastIndexOf("Runoob");
if(lastIndex == - 1){
System.out.println("没有找到字符串 Runoob");
}else{
System.out.println("Runoob 字符串最后出现的位置: "+ lastIndex);
}
}
}
3.Java 实例 - 删除字符串中的一个字符
public class Main {
public static void main(String args[]) {
String str = "this is Java";
System.out.println(removeCharAt(str, 3));
}
public static String removeCharAt(String s, int pos) {
return s.substring(0, pos) + s.substring(pos + 1);
}
}
4.Java 实例 - 字符串替换
public class StringReplaceEmp{
public static void main(String args[]){
String str="Hello World";
System.out.println( str.replace( 'H','W' ) );
System.out.println( str.replaceFirst("He", "Wa") );
System.out.println( str.replaceAll("He", "Ha") );
}
}
5.Java 实例 - 字符串反转
public class StringReverseExample{
public static void main(String[] args){
String string="runoob";
String reverse = new StringBuffer(string).reverse().toString();
System.out.println("字符串反转前:"+string);
System.out.println("字符串反转后:"+reverse);
}
}
6.Java 实例 - 字符串查找
public class SearchStringEmp {
public static void main(String[] args) {
String strOrig = "Google Runoob Taobao";
int intIndex = strOrig.indexOf("Runoob");
if(intIndex == - 1){
System.out.println("没有找到字符串 Runoob");
}else{
System.out.println("Runoob 字符串位置 " + intIndex);
}
}
}
7.Java 实例 - 字符串分割
public class JavaStringSplitEmp {
public static void main(String args[]){
String str = "www-runoob-com";
String[] temp;
String delimeter = "-"; // 指定分割字符
temp = str.split(delimeter); // 分割字符串
// 普通 for 循环
for(int i =0; i < temp.length ; i++){
System.out.println(temp[i]);
System.out.println("");
}
System.out.println("------java for each循环输出的方法-----");
String str1 = "www.runoob.com";
String[] temp1;
String delimeter1 = "\\."; // 指定分割字符, . 号需要转义
temp1 = str1.split(delimeter1); // 分割字符串
for(String x : temp1){
System.out.println(x);
System.out.println("");
}
}
}
8.Java 实例 - 字符串分割(StringTokenizer)
import java.util.StringTokenizer;
public class Main {
public static void main(String[] args) {
String str = "This is String , split by StringTokenizer, created by runoob";
StringTokenizer st = new StringTokenizer(str);
System.out.println("----- 通过空格分隔 ------");
while (st.hasMoreElements()) {
System.out.println(st.nextElement());
}
System.out.println("----- 通过逗号分隔 ------");
StringTokenizer st2 = new StringTokenizer(str, ",");
while (st2.hasMoreElements()) {
System.out.println(st2.nextElement());
}
}
}
9.Java 实例 - 字符串小写转大写
public class StringToUpperCaseEmp {
public static void main(String[] args) {
String str = "string runoob";
String strUpper = str.toUpperCase();
System.out.println("原始字符串: " + str);
System.out.println("转换为大写: " + strUpper);
}
}
10.Java 实例 - 测试两个字符串区域是否相等
public class StringRegionMatch{
public static void main(String[] args){
String first_str = "Welcome to Microsoft";
String second_str = "I work with microsoft";
boolean match1 = first_str.
regionMatches(11, second_str, 12, 9);
boolean match2 = first_str.
regionMatches(true, 11, second_str, 12, 9); //第一个参数 true 表示忽略大小写区别
System.out.println("区分大小写返回值:" + match1);
System.out.println("不区分大小写返回值:" + match2);
}
}
11.Java 实例 - 字符串性能比较测试
public class StringComparePerformance{
public static void main