{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

04-arrayintlist

# 04-arrayintlist - CSE 143 Lecture 4 Implementing...

This preview shows pages 1–6. Sign up to view the full content.

CSE 143 Lecture 4 Implementing ArrayIntList slides created by Marty Stepp http://www.cs.washington.edu/143/

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
2 Exercise • Write a program that reads a file (of unknown size) full of integers and prints the integers in the reverse order to how they occurred in the file. Consider example file data.txt : 17 932085 -32053278 100 3 When run with this file, your program's output would be: 3 100 -32053278 932085 17
3 Solution using arrays int[] nums = new int[100]; // make a really big array int size = 0; Scanner input = new Scanner(new File("data.txt")); while (input.hasNextInt()) { nums[size] = input.nextInt(); // read each number size++; // into the array } for (int i = size - 1; i >= 0; i--) { System.out.println( nums[i] ); // print reversed } 5 size index 0 1 2 3 4 5 6 ... 98 99 value 17 932085 -32053278 100 3 0 0 ... 0 0

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
4 Unfilled arrays int[] nums = new int[100]; int size = 0; • We often need to store an unknown number of values. Arrays can be used for this, but we must count the values. Only the values at indexes [0, size - 1] are relevant. • We are using an array to store a list of values. What other operations might we want to run on lists of values? 5 size index 0 1 2 3 4 5 6 ... 98 99 value 17 932085 -32053278 100 3 0 0 ... 0 0
5 Other possible operations public static void add( int[] list, int size

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

### What students are saying

• As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

Kiran Temple University Fox School of Business ‘17, Course Hero Intern

• I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

Dana University of Pennsylvania ‘17, Course Hero Intern

• The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

Jill Tulane University ‘16, Course Hero Intern