class ArrayTest {
private long [ ] a;
private int nElems;
public ArrayTest ( int max) {
a = new long [ max] ;
nElems = 0 ;
}
public boolean find ( long searchKey) {
int i;
for ( i= 0 ; i< nElems; i++ ) {
if ( a[ i] == searchKey) {
break ;
}
}
if ( i == nElems) {
System. out. println ( "Can't found:" + searchKey) ;
return false ;
} else {
System. out. println ( "find it:" + searchKey) ;
return true ;
}
}
public void insert ( long value) {
a[ nElems] = value;
nElems ++ ;
}
public boolean delete ( long value) {
int i;
for ( i= 0 ; i< nElems; i++ ) {
if ( a[ i] == value) {
break ;
}
}
if ( i == nElems) {
return false ;
} else {
for ( int k= i; k< nElems; k++ ) {
a[ k] = a[ k+ 1 ] ;
}
nElems -- ;
return true ;
}
}
public void findAll ( ) {
for ( int i= 0 ; i< nElems; i++ ) {
System. out. print ( a[ i] + " " ) ;
}
System. out. println ( ) ;
}
public static void main ( String[ ] args) {
ArrayTest a = new ArrayTest ( 100 ) ;
a. insert ( 11 ) ;
a. insert ( 18 ) ;
a. insert ( 16 ) ;
a. insert ( 1 ) ;
a. insert ( 88 ) ;
a. insert ( 66 ) ;
a. insert ( 25 ) ;
a. insert ( 0 ) ;
a. insert ( 35 ) ;
a. insert ( 77 ) ;
a. findAll ( ) ;
a. find ( 88 ) ;
a. delete ( 66 ) ;
a. delete ( 1 ) ;
a. findAll ( ) ;
}
}
package com. zoujc. tes3;
public class ReplaceBlank {
public static String replaceBlankStr ( String str) {
if ( str. length ( ) == 0 || str == null) {
return null;
}
int count = 0 ;
for ( int i= 0 ; i< str. length ( ) ; i++ ) {
if ( str. charAt ( i) == ' ' ) {
count ++ ;
}
}
int oldLen = str. length ( ) ;
int newLen = oldLen + count * 2 ;
char [ ] newChar = new char [ newLen] ;
System. arraycopy ( str. toCharArray ( ) , 0 , newChar, 0 , str. length ( ) ) ;
int i = oldLen - 1 , j = newLen - 1 ;
while ( i >= 0 && i < j) {
if ( newChar[ i] == ' ' ) {
newChar[ j -- ] = 'c' ;
newChar[ j -- ] = 'b' ;
newChar[ j -- ] = 'a' ;
} else {
newChar[ j -- ] = newChar[ i] ;
}
i -- ;
}
String newStr = new String ( newChar) ;
return newStr;
}
public static void main ( String[ ] args) {
String str = "1 2 3 5 8 " ;
str = replaceBlankStr ( str) ;
System. out. println ( str) ;
}
}
package com. zoujc. test2;
public class TurnArray {
public static int getMin ( int [ ] arr) {
int index1 = 0 ;
int index2 = arr. length - 1 ;
int mid = 0 ;
while ( arr[ index1] >= arr[ index2] ) {
if ( index2 - index1 == 1 ) {
mid = index2;
break ;
}
mid = ( index1 + index2) / 2 ;
if ( arr[ index1] == arr[ index2] && arr[ index1] == arr[ mid] ) {
return minInOrder ( arr, index1, index2) ;
}
if ( arr[ mid] >= arr[ index1] ) {
index1 = mid;
} else if ( arr[ mid] <= arr[ index2] ) {
index2 = mid;
}
}
return arr[ mid] ;
}
public static int minInOrder ( int [ ] arr, int index1, int index2) {
int min = arr[ index1] ;
for ( int i= index1 + 1 ; i<= index2; i++ ) {
if ( min > arr[ i] ) {
min = arr[ i] ;
}
}
return min;
}
public static void main ( String[ ] args) {
int [ ] array1 = { 3 , 4 , 5 , 1 , 2 } ;
System. out. println ( getMin ( array1) ) ;
int [ ] array2 = { 3 , 4 , 5 , 1 , 1 , 2 } ;
System. out. println ( getMin ( array2) ) ;
int [ ] array3 = { 3 , 4 , 5 , 1 , 2 , 2 } ;
System. out. println ( getMin ( array3) ) ;
int [ ] array4 = { 1 , 0 , 1 , 1 , 1 } ;
System. out. println ( getMin ( array4) ) ;
int [ ] array5 = { 1 , 2 , 3 , 4 , 5 } ;
System. out. println ( getMin ( array5) ) ;
int [ ] array6 = { 2 } ;
System. out. println ( getMin ( array6) ) ;
int [ ] array7 = { 1 , 1 , 1 , 1 , 1 , 1 , 1 } ;
System. out. println ( getMin ( array7) ) ;
System. out. println ( getMin ( array6) ) ;
}
}