In: Computer Science
Could I please get explainations and working out as to how to get to the answers.
Note that is where part of the answer goes.
1.
Write a statement that removes the first occurrence of item 10 (if any) from an ArrayList of Integer objects, data.
data.remove( );
2.
Complete the following function that returns true if ArrayList list contains any positive (more than 0) item, false otherwise.
public static containsPositive(ArrayList list) {
for(int i=0; i < list.size(); i++) {
if( ) {
; }
} ; }
3.
Consider the following list:
ArrayList list = new ArrayList(Arrays.asList(10, 70, 20, 90, 30, 80, 50, 40, 60));
Complete the following code that gives the following output:
20 90 30 80 50 40 60
<interger> iter = list.listIterator( );
while(iter. ) {
System.out.println( +" ");
}
Ans.) 1. ) data.remove(10)
Because 'remove' removes first occurrence given element from the list. Ex-
Input : list [10, 20, 30, 40, 30] function call to remove 30 from the list: list.remove(30) Output: List elements after removing 30 list [10, 20, 40, 30]
2.)
public static boolean containsPositive(ArrayList list)
{
for(int i=0; i < list.size(); i++)
{
if( (int)list.get(i)>0 )
{
return true ;
}
}
return false ;
}
To access an element in the ArrayList
, use the
get()
method and refer to the index number:
Ex- list= 10,20,30
list.get(1)=20.
Since we want the answer in boolean type i.e. whether the list contain any positive number or not , hence we use the return type of the function as boolean.
We use the 'get' function to access the element of the list and compare whether any digit id positive or not. If it is then it returns true otherwise false. Notic that we used the typecasting to convert the list of object type to integer type '(int)list.get(i)>0'.
Check the sample code for the following problem-
/******************************************************************************
Online Java Compiler.
Code, Compile, Run and Debug java program online.
Write your code in this editor and press "Run" button to execute it.
*******************************************************************************/
import java.util.ArrayList;
public class Main
{
public static boolean containsPositive(ArrayList list)
{
for(int i=0; i < list.size(); i++)
{
if((int) list.get(i)>0)
{
return true ;
}
}
return false ;
}
public static void main(String[] args) {
ArrayList<Integer> list = new ArrayList<Integer>();
list.add(-1);
list.add(-5);
list.add(9);
boolean m= containsPositive(list);
System.out.println(m);
}
}
OUTPUT-
3.)
ListIterator <interger> iter = list.listIterator( 2 );
while(iter. hasNext()) {
System.out.println( iter.next()+" ");
The 'listIterator(int index)' method of java.util.ArrayList class is used to return a list iterator over the elements in this list (in proper sequence), starting at the specified position in the list. The specified index indicates the first element that would be returned by an initial call to next. An initial call to previous would return the element with the specified index minus one.
Here 'list.listIterator( 2 )' means that our list will start from the index 2 i.e element 20 and print all the element after it.
'hasNext()' method Returns true if this scanner or list has another token in its input.
next() method finds and returns the next complete token from this scanner(here list). A complete token is preceded and followed by input that matches the delimiter pattern. This method may block while waiting for input to scan, even if a previous invocation of hasNext() returned true.