7-01奇数求和练习
public class LoopTest {
public static void main ( String[ ] args) {
int sum = 0 ;
for ( int i = 0 ; i <= 100 ; i++ ) {
if ( i % 2 == 1 ) {
sum += i;
}
}
System. out. println ( sum) ;
}
}
7-02水仙花练习功能实现
public class LoopTest_1 {
public static void main ( String[ ] args) {
int bai = 0 ;
int shi = 0 ;
int ge = 0 ;
for ( int i = 100 ; i < 1000 ; i++ ) {
bai = i / 100 ;
shi = i / 10 % 10 ;
ge = i % 10 ;
if ( bai * bai * bai + shi * shi * shi + ge * ge * ge == i) {
System. out. println ( i) ;
}
}
}
}
7-03ASCII编码表
ASCII编码表由来 * 计算机中,所有的数据在存储和运算时都要使用二进制数表示 * a、b、c、d这样的52个字母(包括大写)、以及0、1等数字还有一些常用的符号, 在计算机中存储时也要使用二进制数来表示 * 具体用哪些二进制数字表示哪个符号,当然每个人都可以约定自己的一套(这就叫编码) * 大家如果要想互相通信而不造成混乱,那么大家就必须使用相同的编码规则,于是美国有关的标准化组织就出台了ASCII编码, 统一规定了上述常用符号用哪些二进制数来表示。 字符中重要的ASCII码对应关系 * a : 97 * A : 65 * 0 : 48
7-04char类型的存储
取值范围 * short:占两个字节,是有符号数据,取值范围-32768-32767 * char: 占两个字节,是无符号数据,取值范围0-65536 案例代码
public class ASCIIDemo {
public static void main ( String[ ] args) {
char c = 'a' ;
int i = c + 1 ;
System. out. println ( i) ;
int j = 90 ;
char h = ( char ) j;
System. out. println ( h) ;
System. out. println ( ( char ) 6 ) ;
char k = '你' ;
System. out. println ( k) ;
char m = - 1 ;
}
}
7-05输出所有英文字母
public class LoopTest_2 {
public static void main ( String[ ] args) {
char xiaoXie = 'a' ;
char daXie = 'A' ;
for ( int i = 0 ; i < 26 ; i++ ) {
System. out. println ( xiaoXie+ " " + daXie) ;
daXie++ ;
xiaoXie++ ;
}
}
}
7-06 99乘法表的分析
public class LoopTest_3 {
public static void main ( String[ ] args) {
print99 ( 6 ) ;
}
public static void print99 ( int n) {
for ( int i = 1 ; i <= n; i++ ) {
for ( int j = 1 ; j <= i ; j++ ) {
System. out. print ( j+ "*" + i+ "=" + i* j+ "\t" ) ;
}
System. out. println ( ) ;
}
}
}
7-07实现数组的遍历
public class ArrayMethodTest {
public static void main ( String[ ] args) {
int [ ] arr = { 11 , 44 , 55 , 33 , 66 } ;
printArray ( arr) ;
int [ ] arr2 = { 22 , 88 , 99 , 33 , 66 } ;
printArray ( arr2) ;
}
public static void printArray ( int [ ] arr) {
System. out. print ( "[" ) ;
for ( int i = 0 ; i < arr. length ; i++ ) {
if ( i == arr. length- 1 ) {
System. out. print ( arr[ i] + "]" ) ;
} else {
System. out. print ( arr[ i] + "," ) ;
}
}
System. out. println ( ) ;
}
}
7-08数组逆序
public class ArrayMethodTest_1 {
public static void main ( String[ ] args) {
int [ ] arr = { 3 , 5 , 7 , 1 , 0 , 9 , - 2 } ;
reverse ( arr) ;
printArray ( arr) ;
}
public static void reverse ( int [ ] arr) {
for ( int min = 0 , max = arr. length- 1 ; min < max ; min++ , max-- ) {
int temp = arr[ min] ;
arr[ min] = arr[ max] ;
arr[ max] = temp;
}
}
public static void printArray ( int [ ] arr) {
System. out. print ( "[" ) ;
for ( int i = 0 ; i < arr. length ; i++ ) {
if ( i == arr. length- 1 ) {
System. out. print ( arr[ i] + "]" ) ;
} else {
System. out. print ( arr[ i] + "," ) ;
}
}
System. out. println ( ) ;
}
}
7-09选择排序
原理
第一个元素与该元素后面的数组元素依次比较到数组的最后一个元素,把小的值放在第一个数组元素中,数组循环一圈后,则把最小元素值互换到了第一个元素中。 数组再循环一圈后,把第二小的元素值互换到了第二个元素中。按照这种方式,数组循环多圈以后,就完成了数组元素的排序。这种排序方式我们称为选择排序。
步骤
使用for循环(外层循环),指定数组要循环的圈数(数组循环的圈数=数组长度 - 1) 在每一圈中,通过for循环(内层循环)完成数组要比较的第一个元素与该元素后面的数组元素依次比较到数组的最后一个元素,把小的值放在第一个数组元素中 在每一圈中,要参与比较的第一个元素 = 第几圈循环。 进行第一圈元素比较时,要比较的第一个元素为数组第一个元素,即索引为0的元素 进行第二圈元素比较时,要比较的第一个元素为数组第二个元素,即索引为1的元素 依次类推,得出结论:进行第n圈元素比较时,要比较的第一个元素为数组第n个元素,即数组索引为n-1的元素
案例代码
public class ArrayMethodTest_2 {
public static void main ( String[ ] args) {
int [ ] arr = { 3 , 1 , 4 , 2 , 56 , 7 , 0 } ;
printArray ( arr) ;
}
public static void selectSort ( int [ ] arr) {
for ( int i = 0 ; i < arr. length - 1 ; i++ ) {
for ( int j = i+ 1 ; j < arr. length ; j++ ) {
if ( arr[ i] > arr[ j] ) {
int temp = arr[ i] ;
arr[ i] = arr[ j] ;
arr[ j] = temp;
}
}
}
}
public static void printArray ( int [ ] arr) {
System. out. print ( "[" ) ;
for ( int i = 0 ; i < arr. length ; i++ ) {
if ( i == arr. length- 1 ) {
System. out. print ( arr[ i] + "]" ) ;
} else {
System. out. print ( arr[ i] + "," ) ;
}
}
System. out. println ( ) ;
}
}
7-10冒泡排序
原理 相邻的元素值依次比较,把大的值放后面的元素中,数组循环一圈后,则把最大元素值互换到了最后一个元素中。 数组再循环一圈后,把第二大的元素值互换到了倒数第二个元素中。按照这种方式,数组循环多圈以后,就完成了数组元素的排序。这种排序方式我们称为冒泡排序。 步骤
使用for循环(外层循环),指定数组要循环的圈数(通过图解可知,数组循环的圈数为数组长度 - 1) 在每一圈中,通过for循环(内层循环)完成相邻的元素值依次比较,把大的值放后面的元素中 每圈内层循环的次数,由第几圈循环来决定。 进行第一圈元素比较时,内层循环次数为数组长度 - 1 进行第二圈元素比较时,内层循环次数为数组长度 - 2 依次类推,得出结论:进行第n圈元素比较时,内层循环次数为数组长度 – n 案例代码
public class ArrayMethodTest_2 {
public static void main ( String[ ] args) {
int [ ] arr = { 3 , 1 , 4 , 2 , 56 , 7 , 0 } ;
bubbleSort ( arr) ;
printArray ( arr) ;
}
public static void bubbleSort ( int [ ] arr) {
for ( int i = 0 ; i < arr. length - 1 ; i++ ) {
for ( int j = 0 ; j < arr. length- i- 1 ; j++ ) {
if ( arr[ j] > arr[ j+ 1 ] ) {
int temp = arr[ j] ;
arr[ j] = arr[ j+ 1 ] ;
arr[ j+ 1 ] = temp;
}
}
}
}
public static void printArray ( int [ ] arr) {
System. out. print ( "[" ) ;
for ( int i = 0 ; i < arr. length ; i++ ) {
if ( i == arr. length- 1 ) {
System. out. print ( arr[ i] + "]" ) ;
} else {
System. out. print ( arr[ i] + "," ) ;
}
}
System. out. println ( ) ;
}
}
7-11数组的折半查找
原理 使用数组最中间位置的元素值与要查找的指定数值进行比较,若相等,返回中间元素值的索引; 若不相等,则根据比较的结果,缩小查询范围为上次数组查询范围的一半;再根据新的查询范围,更新最中间元素位置,然后使用中间元素值与要查找的指定数值进行比较 比较结果相等,返回中间元素值的索引;比较结果不相等,继续缩小查询范围为上次数组查询范围的一半,更新最中间元素位置,继续比较,依次类推。 当查询范围缩小到小于0个元素时,则指定数值没有查询到,返回索引值-1。 步骤
定义3个用来记录索引值的变量,变量min记录当前范围最小索引值,初始值为0;变量max记录当前范围最大索引值,初始值为数组长度-1;变量mid记录当前当前范围最中间元素的索引值,初始值为(min+max) / 2 使用循环,判断当前范围下,最中间元素值与指定查找的数值是否相等,若相等,结束循环,返回当前范围最中间元素的索引值mid;若不相等,根据比较结果,缩小查询范围为上一次查询范围的一半 中间元素值 比 要查询的数值大,说明要查询的数值在当前范围的最小索引位置与中间索引位置之间,此时,更新查询范围为: 范围最大索引值 = 上一次中间索引位置 -1; 中间元素值 比 要查询的数值小,说明要查询的数值在当前范围的最大索引位置与中间索引位置之间,此时,更新查询范围为: 范围最小索引值 = 上一次中间索引位置 +1; 在新的查询范围中,更新中间元素值的位置,再次使用最中间元素值与指定查找的数值是否相等。 *中间索引值 = (范围最小索引值 +范围最大索引值) / 2; 每次查询范围缩小一半后,使用if语句判断,查询范围是否小于0个元素,若小于0个元素,则说明指定数值没有查询到,返回索引值-1。
案例代码
public class ArrayMethodTest_3 {
public static void main ( String[ ] args) {
int [ ] arr = { 1 , 3 , 5 , 7 , 9 , 11 , 15 } ;
int index = binarySearch ( arr, 10 ) ;
System. out. println ( index) ;
}
public static int binarySearch ( int [ ] arr, int key) {
int min = 0 ;
int max = arr. length - 1 ;
int mid = 0 ;
while ( min <= max) {
mid = ( min+ max) / 2 ;
if ( key > arr[ mid] ) {
min = mid + 1 ;
} else if ( key < arr[ mid] ) {
max = mid - 1 ;
} else {
return mid;
}
}
return - 1 ;
}
public static int search ( int [ ] arr, int key) {
for ( int i = 0 ; i < arr. length ; i++ ) {
if ( arr[ i] == key) {
return i;
}
}
return - 1 ;
}
}