05. Loops & Arrays
来源:互联网 发布:身份证阅读器软件下载 编辑:程序博客网 时间:2024/06/05 21:47
Switch Statements
Switch (on char, byte, short, int, String, enum)
Most common mistake is leaving out the "break" statement. If you do, control continues into the next case.
Default is optional.
While Loops
While loop tests at the top which means statements in the block may never be executed
do/while loop executes the block at least once
Silent Integer Overflow
Range for int: -2,147,438,648 to 2,147,438,647
For loops
for (initialization; condition; increment/decrement) stmt;
Use a break statement to get out of a loop
Which loop to use
For loop: when the exact number of iterations is known
While loop: when you want to use a sentinel value to terminate
do/while loop: when you want the block executed at least once
Arrays in Memory
- Typically, array elements are stored in adjacent memory cells
- The subscript (or index) is used to calculate an offset to find the desired element
E.g. integer 4 bytes
Address 101418 22 26
Contents 348 1 2
// variable (data) has the starting address (10)
int[] data = {3, 4, 8, 1, 2};
data[3];
What happens behind the scenes is:
10 + 3*4 = 22
- Arithmetric calculation does not depend on the size of data type in an array
That's why subscript (index) numbering starts at 0
Watch out for Off-by-One Errors
- One of the very common mistakes
- Array subscripts (index) start at 0
- But, the number of elements or length starts at 1
Java Enhanced for loop
Simply want to iterate over each element in an array
for (vardecl : array) stmt;
Sample Final Exam Questions
What are main characteristics of arrays in Java?
- Ability to declare a bunch of the same thing (data type) or declare and initialize with an initialization list
- Array has a field, length, and can be accessed by .length
- Access the array with square brackets and subscript numbering (index)
- Array's subscript numbering (index) starts at zero
- Typically, array elements are stored in adjacent memory cells
- The subscript (index) is used to calculate an offset to find the desired element
- Arithmetic calculation does not depend on the size of data types in an array
- Once it is created, its length cannot be changed
- Array index bounds are checked on every access
- 05. Loops & Arrays
- Java Notes: Loops and Arrays & Methods and Class
- loops
- Postscript III:The Operand Stack of PostScript: Arrays, Variables, Loops and Macro Definitions
- Control-Loops
- Nested loops
- Run Loops
- hdu3853 LOOPS
- Run Loops
- hdu3853-LOOPS
- hdu3853 LOOPS
- Fence Loops
- CCAnimate Loops
- Run Loops
- Run loops
- Run Loops
- 【HDU3853】【LOOPS】
- HDU3853-LOOPS
- VMware技巧汇总
- 学点JS实例-文字滑动(4)
- 站军姿(计算几何 多三角形覆盖面积)
- React Native创建项目时报错:npm ERR! Windows_NT 10.0.10240 错误
- PAT:1003. Emergency (25)
- 05. Loops & Arrays
- noip模拟赛(10.4) 背包(pack)
- Linux 设备驱动篇之I2c设备驱动
- effective stl 第41条:理解ptr_fun/mem_fun/mem_fun_ref的由来
- Redis学习必备知识
- Kali Linux 秘籍 第八章 密码攻击
- Java bean 你所知道的一切
- java实现发送手机短信
- 词法分析主程序