#码神心得_06# 运算符、循环流程控制、数组
2016-06-03 19:43
197 查看
这一次课的内容挺多的,虽然以前基本都有学过,但是“温故而知新”,收获也是蛮大的!
一、运算符
1、算术运算符:+、-、*、/、%等2、关系运算符:>=、<=、==、===(恒等于)
3、逻辑运算符:&&、||、!、(与或非)
4、赋值运算符:==、+=、-=等
5、位运算符:针对2进制数
& 与:同时为1返回1
| 或:只要有1返回1
~ 非:翻转所有位,一元运算
^ 异或:只要不相同返回1
<< 左移:向左移动2位,向右补0
>>右移:向右移动2位,向左补符号位
>>>无符号位右移:向右移动2位,向左补0
6、三目运算符:a>b?a:b
7、其他运算符:对象运算符instanceof,判断该对象是否为指定对象,返回true或false
二、循环流程控制
一般情况下,三种循环结构都可以互相转换1、while:
2、do-while
3、for
4、foreach:在遍历数组、集合方面使用较方便
int[] temp = {3,7,9,6,12,4,30,45,20,89}; for (int i : temp) { System.out.print(i+" "); }
5、关键字:
break:终止整个循环
continue:跳过本次循环
return:跳出这个方法
三、数组
1、数组的声明:静态声明:int[] a = {1,2,3,4,5}; 动态声明:int[] a = new int[5];只声明数组长度2、数组的遍历:使用循环结构,由数组下标进行遍历:
int[] temp = {3,7,9,6,12,4,30,45,20,89}; for (int i = 0; i < temp.length; i++) { System.out.print(temp[i]+" "); }3、数组里的数据类型不是绝对要一致,例如声明一个对象数组,里面的对象可以是一个父类的不同的子类对象。
4、数组出错类型:
(1)超出界限j:ava.lang.ArrayIndexOutOfBoundsException
(2)内存溢出:exceeds VM limit
(3)数组元素个数超出最大值:Requested array size
5、Arrays类的一些用法:
(1)查找数组元素的下标:int index = Arrays.binarySearch(目标数组, 目标元素);
(2)复制数组:int[] temp = Arrays.copyOf(目标数组,长度);
(3)输出数组:Arrays.toString(目标数组);
(4)改变数组元素:Arrays.fill(temp, 3,6, 100);将数组temp下标3—6的元素都改为100
(5)对数组进行升序排序:Arrays.sort(temp);
课堂作业
作业一:使用循环语句输出九九乘法表
public class MultiplicationTable { public static void main(String[] args) { String result ; for (int i = 1; i < 10; i++) { for (int j = 1; j <= i; j++) { result = i + "*" + j + "=" + (i * j); if (i == j) { System.out.print(result); } else { System.out.print(result + ","); } } System.out.println(); } } }效果截图:
作业二:使用循环输出等腰三角形
public class Triangle { public static void main(String[] args) { System.out.println("请输入等腰三角形高:"); Scanner scanner = new Scanner(System.in); int length = scanner.nextInt(); int temp = 0; for (int i = 0; i < length; i++) { for (int j = 0; j < length * 2 - 1; j++) { if (j + 1 < (length - temp) || j + 1 > (length + temp)) { System.out.print(" "); } else { System.out.print("*"); } } temp++; System.out.println(); } } }效果截图:
作业三:请将下面的数组去重,并输出最后结果,可能的话封装成一个通用的方法
String [] str = {“Java”, “C++”, “Php”, “C#”, “Python”, “C++”, “Java”};public class DuplicationRemove { public static void main(String[] args) { String[] str = { "Java", "C++", "Php", "C#", "Python", "C++", "Java" }; DuplicationRemove du = new DuplicationRemove(); String[] result = du.removeDuplication(str); System.out.println("原数组为:" + Arrays.toString(str)); System.out.println("\n去重后为:" + Arrays.toString(result)); } public String[] removeDuplication(String[] str) { String tempstr = ""; for (int i = 0; i < str.length; i++) { int temp = 0; for (int j = 0; j < i; j++) { if (str[i].equals(str[j])) { temp = 1; break; } } if (temp == 0) { tempstr += str[i] + " "; } } String[] result = tempstr.split(" "); return result; } }效果截图:
相关文章推荐
- 基于Theano的多层神经网络及其实现(一)
- [知其然不知其所以然-28] Understanding hibernation - temporary mapping
- NSString 处理技巧:分割字符串
- Lua入门教程 2.类型与值
- 学习thinkphp中自带的http.class.php工具类
- Cocos2D与SpriteBuilder的问题在哪提问
- ext 4.0 自定义模板
- python 学习day4
- hdu 2354 Integer Intervals【Bfs+暴力】
- 项目-两个成员的类模板为该类增默认构造函数、带两个参数分别对应两个数据成员初值的构造函数,以及复制构造函数
- Cocos2D与SpriteBuilder的问题在哪提问
- Cocos2D与SpriteBuilder的问题在哪提问
- 图的概念、存储结构及遍历
- Gradle配置多渠道打包
- 线程1—Runnable
- Android分别通过代码和xml实现动画
- spring 使用注解获取properties 文件属性值
- 自学——关于实现双指缩放的学习总结
- cocos2dx游戏--欢欢英雄传说--添加动作
- 强化学习基本方法(三)