CodeGym /Java Course /Java Syntax /Practice with ArrayList

Practice with ArrayList

Java Syntax
Level 7 , Lesson 6
Available

"You're just sitting idle again? You're a robot! Robots do something all the time. Here are a couple of exercises to keep you from rusting. But first, a few hints:"

"Hint 1:

By 'list', we usually mean an ArrayList."

"Hint 2:

A 'string' means a String object."

"Hint 3:

'Create a list of strings' most often means the following: ArrayList&ltString> list = new ArrayList&ltString>();"

7
Task
New Java Syntax, level 7, lesson 6
Locked
Everything has a root
The printSqrt(int[] array) method should print the square root of each element in the passed array. But this isn't happening due to conflicting variable names. Correct the variable names so that the code compiles. The program should display a suitable line on the console for each element of the arra
7
Task
New Java Syntax, level 7, lesson 6
Locked
Giant cities
The program should display the population of the largest cities in the world and compare them with Tokyo, the most populous city of all. But the program's algorithm is slightly broken. To correct the error, you need to make it so the line that displays information about the world's largest city uses
Comments (82)
TO VIEW ALL COMMENTS OR TO MAKE A COMMENT,
GO TO FULL VERSION
Fadhil Radhian Level 18, Semarang, Indonesia
25 March 2023
great exercises to solidify ArrayList concept !
Ozkin Level 20, Colombia Expert
12 November 2022
hint: pueden ayudarse de indexof() hint: indexOf() is usefull
Spydrow Level 14, Germany, Germany
26 April 2022
Can somebody please explain, why I get 6 as the highest number? BufferedReader reader = new BufferedReader(new InputStreamReader(System.in)); ArrayList<Integer> list = new ArrayList<Integer>(); for (int i = 0; i < 5; i++) { list.add(Integer.parseInt(reader.readLine())); } int highestNumber = list.get(0); System.out.println(list); for (int i = 0; i < list.size(); i++) { for (int j = i; j < list.size(); j++) { if (list.get(i) >= list.get(j)) { highestNumber = list.get(i); } else{ highestNumber = list.get(j); } } } System.out.println(highestNumber); } }
AfEi Level 19
12 May 2022
First, there is no need to use nested loops to find largest number. Second, the code below works incorrectly. if (list.get(i) >= list.get(j)) highestNumber = list.get(i); else highestNumber = list.get(j); For example: list = [7,7,4,9,6] when j = 3, highestNumber = 9 when j =4, list.get(0) >= list.get(4), highestNumber = 6
Spydrow Level 14, Germany, Germany
13 May 2022
Ah ok, I see what you mean. Good thing the last number wasn't 9 because then I would've gotten the "correct" result and be totally confused why the program refused validation :))
juxhinb7 Level 16, Germany, Germany
27 January 2022
I think there is a bug in the exercise "The largest number" because the Requirements say first of all to read 5 numbers from the keyboard and write them to the integers list, which my program does and second of all to display the maximum number on the screen which my program also does and the maximum number gets shown in the output, so i don't get it. Why do i not pass the test? Or is it that the integers list cannot hold the primitive data type int, but only the object Integer, but if that were the case the maximum number would not get displayed in the output, and i would get an error, right? This is my code: package en.codegym.task.jdk13.task07.task0708; import java.io.BufferedReader; import java.io.InputStreamReader; import java.util.ArrayList; /* The largest number */ public class Solution { private static ArrayList<Integer> integers = new ArrayList<>(); public static void main(String[] args) throws Exception { //write your code here BufferedReader reader = new BufferedReader(new InputStreamReader(System.in)); int i = 0; while (i < 5) { integers.add(Integer.parseInt(reader.readLine())); i++; } int maximumNumber = 0; for (int item : integers) { if (item > maximumNumber) { maximumNumber = item; } } System.out.println(maximumNumber); } }
whoseunassailable Level 28, India, India
27 January 2022
i did the same mistake as you did and here's the thing. There's only one mistake in your code. Your logic is perfectly right but just the thing where you are supposed to be wrong is the value of int maximumNumber = 0. What if i choose a negative number in my items. See that's the flaw. Just replace the value 0 with lowest value. I hope you know how to do that. If you don't then tag me again and i will be more than happy to help you.
juxhinb7 Level 16, Germany, Germany
28 January 2022
Oh i finally get it now, thank you!
Jcode Level 27, United Kingdom
28 January 2022
Your observation is correct. I and probably others saw the list of positive numbers and coded to find the max number in that list.
Jonaskinny Level 25, Redondo Beach, United States
15 February 2022
could try getting integer's internal Sting[], using Arrays.sort, then printing the string at the array[length-1]
Anonymous #10887977 Level 14, Chicago, United States
9 March 2022
I would just assign the value of maximumNumber arbitrarily to the first element like this... int maximumNumber = integers.get(0); Then you can compare the other elements to that one while looping through the list.
Hoist Level 35, San Diego, United States
6 January 2022
Here: https://codegym.cc/help/4942
Sajjad Ahmed Level 10, Bangalore, India
20 January 2022
? This is not the solution for this question
Sajjad Ahmed Level 10, Bangalore, India
19 September 2021
package com.codegym.task.task07.task0708; import java.io.BufferedReader; import java.io.InputStreamReader; import java.util.ArrayList; import java.util.List; /* Longest string */ public class Solution { private static List<String> strings; static int c_len = 0, o_len = 0; public static void main(String[] args) throws Exception { Solution.strings = new ArrayList<>(); ArrayList<String> lstr = new ArrayList<>(); for (int i = 0; i < 5; i++) { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); Solution.strings.add(br.readLine()); } for ( int i = 0; i < strings.size(); i++){ c_len = strings.get(i).length(); if ( c_len > o_len ){ o_len = c_len; for ( int j = lstr.size()-1; j >= 0; j--){ lstr.remove(j); } lstr.add(0,strings.get(i)); } else if(c_len == o_len){ lstr.add(strings.get(i)); } } for ( int j = 0; j < lstr.size(); j++){ System.out.println(lstr.get(j)); } } } This is working but validation failed
Justin Smith Level 41, Greenfield, USA, United States
10 July 2021
For "Longest String" there are definitely methods that will work but fail validation. For example, creating a second Arraylist to store only the strings that are tied for longest will fail validation, even if it works in testing for all numbers of tied strings. My guess is that the validator is expecting to find the variable "strings" in the System.out.
ScaredGerd Level 10, Hamburg, Germany
2 September 2021
For me it worked with a second array and passed the validation. No variable "strings" in the output-loop:

for (String s : longList) {
            System.out.println(s);
        }
Angelo Tratsis Level 8, Boston, United States
2 June 2021
Never give up my friends. Keep on pushing
Maksim Lukjanskis Level 17, Riga, Latvia
30 November 2020
(String str : list), someone can explain me what does " : " mean?
Ron R Level 2, Washington, United States
3 December 2020
The ":" basically means "in". If you were to read that expression you'd say, for each "str" element of datatype "String" in "list", do the following.
Neil Hainer Level 25, Mount Laurel, United States
19 November 2020
Expressing ourselves more concisely: What's wrong with this statement? The verifery tells me: List<String> myStrings = new ArrayList<String>(); "Declare a string list variable and immediately initialize it." Failed. I don't see the problem. The code runs correctly.
Neil Hainer Level 25, Mount Laurel, United States
19 November 2020
I solved it. The verifier wanted: ArrayList<String> myStrings = new ArrayList<String>(); not: List<String> myStrings = new ArrayList<String>(); Although both ways work with the java compiler.