Section1Problems 2 - 3. indexOf returns method cafled that...

Info iconThis preview shows page 1. Sign up to view the full content.

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

Unformatted text preview: 3. indexOf returns method cafled that t.he index of a particular Write a static return in an array of ints. The method should t.he index of the firsl value value in the array. If the value is not in the of the Larget occurrence -1. For example, Lf an array list stores the it called shoufd return array, val-ues: following tol ----T Iistl+-+--->1421 ----r tll t2l t3l t4l +----+----+----+----+----+----+----+----+----+----+ 7 l-9114 Bl39l42 | | +----+----+----+----+----+----+----+----r----r----* tsl t6l tlt Bl19l t8l tel 0l 4 because the index of the indexOf(list, 8) should return Then the call is at index 4. The caII of the value 8 in the array occurrence first -1 because the value 2 is not in the array. 2) shouLd return indexOf(Iist, method print that takes an array of ints as a parameter and a static Write prints to System.out as a.bracketed, the contents of the array that Lf a variable stores Iist. For example, cal-Ied Iist the comma-separated values: fol]owing (3, Then Lhe print should Lg, call: (list) i the 21 , should 4, following 98, 304, output: -9, 721 line of output. 21 , 4, 98, 304, -9, 121 produce [3, 19, Your 1. method produce a complete takes a sorted array of integers method cal-led numUni-que that Wrrce a static returns the number of unique walues in the array. and that as a parameter Tl'ra Arr:\, which means that order, duplicates is guaranteed to be in sorted Lf a variable called stores For example, wiLl be grouped together. "list" values: the foflowinq (5, 7, 7, 7, 8, 22, 22, 23, 31, 35, 35, 40, 40, 40, 4L) then the calI (5, J, values method should ior the folfowing leclared: numUnique(Iist) 8, 22, 23, 31, return 0. problems, 9 because this Iist has 9 unj-que. should return 35, 40 and 41). If passed an empty listr lour you have the fol-l-owing variables assume that int el-ementData 0; int size new int[100]; we wiII values. store a Iist of integer Jsing these two variables, -l I a€ iu ro e hr - :snuAunei r1L- ) / O!I U! e o f 1 0 0 , b u t w e m i g h t n o t b e "uaJ.ri r^raY ras a capacity size keeps track of how many elements are The variabl-e liven time. (initialized . to O above to form an empty list) -n the list Assuming add the you given have a value variable to the val-ue called list. end of the of r-rrna e_Y !/v LLLvf The array rr At qL an / q r rt y currently in{- .-,-ite code that would you have a variabl-e index called of Assuming index, value at the allthe removes preserving their order. and otherwise Eype int, subsequent wrj-te code values to that the left Assuming you af t-rrno inf ejrFv !..Lt have a variable write code that tO the rioht val-ue and a variabfe called index both caLled adds the value at the index, shifting aII ancl rtt. hort^ri ca a-".i -.!L^ l SUbSeqUent valUeS ...
View Full Document

This note was uploaded on 11/03/2009 for the course CSE 143 taught by Professor Sr during the Spring '08 term at University of Washington.

Ask a homework question - tutors are online