CSE 14 Lecture Notes - Lecture 12: Standard Streams

52 views5 pages
10 May 2018
School
Course
Professor
CMPS12A Lecture 12: More Arrays & Introduction to Input/Output Streams
There is a little bit more about how arrays work. One thing for sure is how one array can be
opied to aothe aa sie ou a’t just ake a aa aiale eual aothe aa
variable. Thus, what you have to do is:
Example:
class copyArray {
public static void main(String[] args) {
int [] x = {0, 1, 2, 3};
int [] y = new int[x.length];
y = x; //Does not work so take this line off
for (int i=0; i<x.length; i++)
y[i] = x[i];
“ste.out.pitlFo :  + [] +   + [] +   + [] +   + [];
“ste.out.pitlFo :  + [] +   + [] +   + [] +   + [];
y[2] = 10;
y[1] = 20;
“ste.out.pitlFo :  + [] +   + [] +   + [] +   + [];
“ste.out.pitlFo :  + [] +   + [] +   + [] +   + [];
}
}
You’ll see that  doig the fo loop ad opig eah eleet of  dietl ito , e get a
op of it hee oth aiales do’t efeee the sae aa. “o i the fist ouput, it pits
out:
OUTPUT
0 1 2 3
0 1 2 3
find more resources at oneclass.com
find more resources at oneclass.com
Unlock document

This preview shows pages 1-2 of the document.
Unlock all 5 pages and 3 million more documents.

Already have an account? Log in
In the second output:
OUTPUT
0 1 2 3
0 20 10 3
Thus, we did succeed in making a unique copy of the array. Each array is unique and does not
reference each other at the same time. Notice that for the y array, we need to initiate how
much space there should be to store items. Since we want another copy of the x array, we
ake the ’s aa stoage e the sae length as x, so x.length.
Another example with arrays but using functions:
class arrayFunctions {
p s v m() { //puli stati…
int [] x = {1, 4, 6, 10};
int [] y = new int[x.length];
copyArray(x, 0, y, x.length);
printArray(x);
printArray(y);
}
static void printArray(int[] a) {
for (int i = 0; i < a.length; i++) {
“ste.out.pita[i] +  ;
System.out.println();
}
static int [] copyArray(int[] a, int n, int[] b, int m) {
for (int i = n; i < m; i++)
b[i] = a[i];
find more resources at oneclass.com
find more resources at oneclass.com
Unlock document

This preview shows pages 1-2 of the document.
Unlock all 5 pages and 3 million more documents.

Already have an account? Log in

Document Summary

Cmps12a lecture 12: more arrays & introduction to input/output streams. There is a little bit more about how arrays work. One thing for sure is how one array can be (cid:272)opied to a(cid:374)othe(cid:396) a(cid:396)(cid:396)a(cid:455) si(cid:374)(cid:272)e (cid:455)ou (cid:272)a(cid:374)"t just (cid:373)ake a(cid:374) a(cid:396)(cid:396)a(cid:455) (cid:448)a(cid:396)ia(cid:271)le e(cid:395)ual a(cid:374)othe(cid:396) a(cid:396)(cid:396)a(cid:455) variable. (cid:455)ste(cid:373). out. p(cid:396)i(cid:374)tl(cid:374)(cid:894)(cid:862)fo(cid:396) (cid:454): (cid:862) + (cid:455)[(cid:1004)] + (cid:862) (cid:862) + (cid:455)[(cid:1005)] + (cid:862) (cid:862) + (cid:455)[(cid:1006)] + (cid:862) (cid:862) + (cid:455)[(cid:1007)](cid:895); y[2] = 10; y[1] = 20; (cid:455)ste(cid:373). out. p(cid:396)i(cid:374)tl(cid:374)(cid:894)(cid:862)fo(cid:396) (cid:454): (cid:862) + (cid:454)[(cid:1004)] + (cid:862) (cid:862) + (cid:454)[(cid:1005)] + (cid:862) (cid:862) + (cid:454)[(cid:1006)] + (cid:862) (cid:862) + (cid:454)[(cid:1007)](cid:895); (cid:455)ste(cid:373). out. p(cid:396)i(cid:374)tl(cid:374)(cid:894)(cid:862)fo(cid:396) (cid:454): (cid:862) + (cid:455)[(cid:1004)] + (cid:862) (cid:862) + (cid:455)[(cid:1005)] + (cid:862) (cid:862) + (cid:455)[(cid:1006)] + (cid:862) (cid:862) + (cid:455)[(cid:1007)](cid:895); You"ll see that (cid:271)(cid:455) doi(cid:374)g the fo(cid:396) loop a(cid:374)d (cid:272)op(cid:455)i(cid:374)g ea(cid:272)h ele(cid:373)e(cid:374)t of (cid:454) di(cid:396)e(cid:272)tl(cid:455) i(cid:374)to (cid:455), (cid:449)e get a (cid:272)op(cid:455) of it (cid:449)he(cid:396)e (cid:271)oth (cid:448)a(cid:396)ia(cid:271)les do(cid:374)"t (cid:396)efe(cid:396)e(cid:374)(cid:272)e the sa(cid:373)e a(cid:396)(cid:396)a(cid:455). O i(cid:374) the fi(cid:396)st ouput, it p(cid:396)i(cid:374)ts out:

Get access

Grade+20% off
$8 USD/m$10 USD/m
Billed $96 USD annually
Grade+
Homework Help
Study Guides
Textbook Solutions
Class Notes
Textbook Notes
Booster Class
40 Verified Answers
Class+
$8 USD/m
Billed $96 USD annually
Class+
Homework Help
Study Guides
Textbook Solutions
Class Notes
Textbook Notes
Booster Class
30 Verified Answers

Related Documents

Related Questions