Skip to content

Commit

Permalink
数组“
Browse files Browse the repository at this point in the history
  • Loading branch information
sotpngu committed Jul 19, 2020
1 parent c5483a0 commit 51713af
Show file tree
Hide file tree
Showing 31 changed files with 310 additions and 0 deletions.
75 changes: 75 additions & 0 deletions src/main/java/examples/Array/MainClass.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
package examples.Array;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;

public class MainClass {

public static void main(String[] args) throws Exception {
int array[] = {2, 5, -2, 6, -3, 8, 0, -7, -9, 4 };
Arrays.sort(array);
printArray("排序结果为:", array);
int index = Arrays.binarySearch(array, 2);
System.out.println("元素 2 在第 " + index + " 个位置");

//插入数组
int newIndex = index;
array = insertElement(array, 1, newIndex);
printArray("数组添加元素 1", array);

//数组长度
String[][] data = new String[2][5];
System.out.println("第一维数组长度: " + data.length);
System.out.println("第二维数组长度: " + data[0].length);

//数组反转
ArrayList<String> arrayList = new ArrayList<String>();
arrayList.add("A");
arrayList.add("B");
arrayList.add("C");
Collections.reverse(arrayList);
System.out.println("反转后排序:" + arrayList);


//获取最大值、最小值
Integer[] numbers = { 8, 2, 7, 1, 4, 9, 5 };
int min = (int) Collections.min(Arrays.asList(numbers));
int max = (int) Collections.max(Arrays.asList(numbers));
System.out.println("最小值:" + min);
System.out.println("最大值:" + max);


//数组合并
String a[] = { "A", "E", "I" };
String b[] = { "O", "U" };



}

private static void printArray(String message, int array[]) {
System.out.println(message
+ ": [length: " + array.length + "]");
for (int i = 0; i < array.length; i++) {
if(i != 0){
System.out.print(", ");
}
System.out.print(array[i]);
}
System.out.println();
}

private static int[] insertElement(int original[],
int element, int index) {
int length = original.length;
int destination[] = new int[length + 1];
System.arraycopy(original, 0, destination, 0, index);
destination[index] = element;
System.arraycopy(original, index, destination, index
+ 1, length - index);
return destination;
}

}
24 changes: 24 additions & 0 deletions src/main/java/examples/String/JavaStringSplitEmp.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
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("");
}
}
}
23 changes: 23 additions & 0 deletions src/main/java/examples/String/JavaStringSplitEmp2.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package examples.String;

import java.util.StringTokenizer;

public class JavaStringSplitEmp2 {
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 changes: 9 additions & 0 deletions src/main/java/examples/String/Main.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
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);
}
}
11 changes: 11 additions & 0 deletions src/main/java/examples/String/SearchStringEmp.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
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);
}
}
}
11 changes: 11 additions & 0 deletions src/main/java/examples/String/SearchlastString.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
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);
}
}
}
20 changes: 20 additions & 0 deletions src/main/java/examples/String/StringCompareEmp.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package examples.String;

public class StringCompareEmp {

public static void main(String[] args) {
String str = "Hello world";
String anotherString = "Hello world";
Object objStr = str;

// 以下实例中我们通过字符串函数
// compareTo (string) ,
// compareToIgnoreCase(String)
// 及 compareTo(object string) 来比较两个字符串,
// 并返回字符串中第一个字母ASCII的差值。
System.out.println(str.compareTo(anotherString));
System.out.println(str.compareToIgnoreCase(anotherString));
System.out.println(str.compareTo(objStr.toString()));
}

}
22 changes: 22 additions & 0 deletions src/main/java/examples/String/StringComparePerformance.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
public class StringComparePerformance{
public static void main(String[] args){
long startTime = System.currentTimeMillis();
for(int i=0;i<50000;i++){
String s1 = "hello";
String s2 = "hello";
}
long endTime = System.currentTimeMillis();
System.out.println("通过 String 关键词创建字符串"
+ " : "+ (endTime - startTime)
+ " 毫秒" );
long startTime1 = System.currentTimeMillis();
for(int i=0;i<50000;i++){
String s3 = new String("hello");
String s4 = new String("hello");
}
long endTime1 = System.currentTimeMillis();
System.out.println("通过 String 对象创建字符串"
+ " : " + (endTime1 - startTime1)
+ " 毫秒");
}
}
34 changes: 34 additions & 0 deletions src/main/java/examples/String/StringConcatenate.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
public class StringConcatenate {
public static void main(String[] args){


long startTime = System.currentTimeMillis();
for(int i=0;i<5000;i++){
String result = "This is"
+ "testing the"
+ "difference"+ "between"
+ "String"+ "and"+ "StringBuffer";
}
long endTime = System.currentTimeMillis();
System.out.println("字符串连接"
+ " - 使用 + 操作符 : "
+ (endTime - startTime)+ " ms");


long startTime1 = System.currentTimeMillis();
for(int i=0;i<5000;i++){
StringBuffer result = new StringBuffer();
result.append("This is");
result.append("testing the");
result.append("difference");
result.append("between");
result.append("String");
result.append("and");
result.append("StringBuffer");
}
long endTime1 = System.currentTimeMillis();
System.out.println("字符串连接"
+ " - 使用 StringBuffer : "
+ (endTime1 - startTime1)+ " ms");
}
}
9 changes: 9 additions & 0 deletions src/main/java/examples/String/StringFormat.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import java.util.*;

public class StringFormat {
public static void main(String[] args){
double e = Math.E;
System.out.format("%f%n", e);
System.out.format(Locale.CHINA , "%-10.4f%n%n", e); //指定本地为中国(CHINA)
}
}
36 changes: 36 additions & 0 deletions src/main/java/examples/String/StringOptimization.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
public class StringOptimization {
public static void main(String[] args){
String variables[] = new String[50000];


for( int i=0;i <50000;i++){
variables[i] = "s"+i;
}
long startTime0 = System.currentTimeMillis();
for(int i=0;i<50000;i++){
variables[i] = "hello";
}
long endTime0 = System.currentTimeMillis();
System.out.println("直接使用字符串: "+ (endTime0 - startTime0) + " ms" );


long startTime1 = System.currentTimeMillis();
for(int i=0;i<50000;i++){
variables[i] = new String("hello");
}
long endTime1 = System.currentTimeMillis();
System.out.println("使用 new 关键字:" + (endTime1 - startTime1) + " ms");



long startTime2 = System.currentTimeMillis();
for(int i=0;i<50000;i++){
variables[i] = new String("hello");
variables[i] = variables[i].intern();
}
long endTime2 = System.currentTimeMillis();
System.out.println("使用字符串对象的 intern() 方法: "
+ (endTime2 - startTime2)
+ " ms");
}
}
12 changes: 12 additions & 0 deletions src/main/java/examples/String/StringRegionMatch.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
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);
}
}
8 changes: 8 additions & 0 deletions src/main/java/examples/String/StringReplaceEmp.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
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") );
}
}
8 changes: 8 additions & 0 deletions src/main/java/examples/String/StringReverseExample.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
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);
}
}
8 changes: 8 additions & 0 deletions src/main/java/examples/String/StringToUpperCaseEmp.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
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);
}
}
Binary file added target/classes/Applet/HelloWorldApplet.class
Binary file not shown.
Binary file added target/classes/JavaStringSplitEmp.class
Binary file not shown.
Binary file added target/classes/Main.class
Binary file not shown.
Binary file added target/classes/SearchStringEmp.class
Binary file not shown.
Binary file added target/classes/SearchlastString.class
Binary file not shown.
Binary file added target/classes/StringComparePerformance.class
Binary file not shown.
Binary file added target/classes/StringConcatenate.class
Binary file not shown.
Binary file added target/classes/StringFormat.class
Binary file not shown.
Binary file added target/classes/StringOptimization.class
Binary file not shown.
Binary file added target/classes/StringRegionMatch.class
Binary file not shown.
Binary file added target/classes/StringReplaceEmp.class
Binary file not shown.
Binary file added target/classes/StringReverseExample.class
Binary file not shown.
Binary file added target/classes/StringToUpperCaseEmp.class
Binary file not shown.
Binary file added target/classes/examples/Array/MainClass.class
Binary file not shown.
Binary file not shown.
Binary file not shown.

0 comments on commit 51713af

Please sign in to comment.