I am having problems with this Arraylist, as I am not sure on how to do it. JAVA
ID: 3678875 • Letter: I
Question
I am having problems with this Arraylist, as I am not sure on how to do it. JAVA please. This is what I have done so far. I have created an Arraylist with 10 integers, then incremented each element by 1. I have provided the code and a snippet of the correct output.
This what I need help on:
This will be in BOLD in the code. With this new Arraylist, a for loop has been created. It must search for 60, and display that it was not found. It must search for 61, and display that it was found at the index that it is located in.
//The Arraylist Code
import java.util.ArrayList;
public class P8 {
public static void main(String[] args) {
System.out.println("Written by David Kirk. ");
// Create an empty ArrayList of integers.
ArrayList numbers = new ArrayList<>();
// Loop through elements.
for (int i = 0; i < numbers.size(); i++) {
int value = numbers.get(i);
}
// Add 10 numbers (10, 20, ...100) by using a loop.
numbers.add(10);
numbers.add(20);
numbers.add(30);
numbers.add(40);
numbers.add(50);
numbers.add(60);
numbers.add(70);
numbers.add(80);
numbers.add(90);
numbers.add(100);
// Display the elements by using an enhanced for loop
for (int value : numbers) {
System.out.print(value+" ");
}
System.out.println();
// Increment each element of the arraylist by 1 in a traditional loop.
for (int i = 0; i < numbers.size(); i ++)
{
int oldVal = numbers.get(i);
int newVal = oldVal + 1;
numbers.set(i, newVal);
// Display the elements by simply using println.
System.out.print(newVal+", ");
}
for(int i=60; i<=61; i++){
// COMPLETE. IN THIS LOOP, AFTER YOU FIND THE VALUE, REMOVE IT.
}
System.out.println();
System.out.println("was not found.");
System.out.println("was found at index");
// Display the elements by simply using println.
System.out.println();
System.out.println(numbers);
}
}
The output snippet:
10 20 30 40 50 60 70 80 90 100 [11, 21, 31, 41, 51, 61, 71, 81, 91, 101] 60 was not found. 61 was found at index 5 [11, 21, 31, 41, 51, 71, 81, 91, 101]Explanation / Answer
Hello Here is the modified code:
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package practice;
import java.util.ArrayList;
public class P8 {
public static void main(String[] args) {
System.out.println("Written by David Kirk. ");
String result="";
// Create an empty ArrayList of integers.
ArrayList<Integer> numbers = new ArrayList(); // Array list of type integers
for(int i=0;i<10;i++)
{
numbers.add((i+1)*10);
}
// Display the elements by using an enhanced for loop
for (int value : numbers) {
System.out.print(value+" ");
}
System.out.println();
// Increment each element of the arraylist by 1 in a traditional loop.
for (int i = 0; i < numbers.size(); i ++)
{
int oldVal = numbers.get(i);
int newVal = oldVal + 1;
numbers.set(i, newVal);
// Display the elements by simply using println.
System.out.print(newVal+", ");
}
int count=0;
for(int i=0;i<numbers.size();i++)
{
if(numbers.get(i)==60)
{
System.out.println("Number 60 found");
}
else
count++; // count for only single output
if(count==10)
{
System.out.println();
System.out.println("Number 60 was not found.");
}
if(numbers.get(i)==61)
{
result="Number 61 was found at index:"+i; // Storing string in result to print after "60 not found"
}
}
System.out.println();
System.out.println(result);
// Display the elements by simply using println.
System.out.println();
System.out.println(numbers);
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.