CodeGym
CodeGym University
Learning
Course
Tasks
Surveys & Quizzes
Games
Help
Schedule
Community
Users
Forum
Chat
Articles
Success stories
Activity
Reviews
Subscriptions
Light theme
Question
  • Reviews
  • About us
Start
Start learning
Start learning now
  • All questions
Maksim Krasnov
Level 42
Kyiv
  • 20.06.2022
  • 202views
  • 3comments

error message: In the main(String[]) method, use a call to the Arrays.binarySearch(int[], int) method. Although i use Arrays.binarySearch(int[], int) ???

Question about the task Is anyone there?
Java Syntax,  Level 6,  Lesson 8
Resolved

Implement the main(String[]) method, which displays true if the element is contained in the passed array, otherwise false.
The array variable should not change the position of its elements.

To search for an element in an array, you need a binary search. To do this, use the static Arrays.binarySearch(int[], int) method of the Arrays class.
In the first argument, pass a sorted array to be searched. The second parameter is the element to be searched for.
The Arrays.binarySearch(int[], int) method returns the index of the query element, if such an element exists in the array.

You can use the Arrays.sort(int[]) method to sort the array.
During testing, the values of the variables in the Solution class will be different.

Requirements:
  • In the main(String[]) method, do not change the position of the elements in the array.
  • When the main(String[]) method is called, display true if the value of the element variable is contained in the array.
  • When the main(String[]) method is called, display false if the passed element is not contained in the array.
  • In the main(String[]) method, use a call to the Arrays.binarySearch(int[], int) method.
package en.codegym.task.pro.task05.task0519; import java.util.Arrays; /* Is anyone there? */ public class Solution { public static int[] array = {9, 8, 7, 6, 5, 4, 3, 2, 1}; public static int element = 5; public static void main(String[] args) { //write your code here int[] tmpArray = Arrays.copyOfRange(array, 0, array.length); Arrays.sort(tmpArray); int idx = Arrays.binarySearch(tmpArray, element); boolean contained = idx >= 0; System.out.println(contained); } }
0
Comments (3)
  • Popular
  • New
  • Old
You must be signed in to leave a comment
Lisa L
Level 47 , Nuremberg, Germany
20 June 2022, 15:50solution
have you tried copyOf instead of copyOfRange? I know the result is the same but sometimes CG requires some certain path to the solutionl
+5
Maksim Krasnov
Level 42 , Kyiv, Ukraine
20 June 2022, 18:27
Thank you. The test passes.
+1
Guadalupe Gagnon
Level 37 , Tampa, United States
20 June 2022, 15:08
Looking at the comments on the task page it looks like this task is bugged.
+1
Learn
  • Registration
  • Java Course
  • Help with Tasks
  • Pricing
  • Game Projects
  • Java Syntax
Community
  • Users
  • Articles
  • Forum
  • Chat
  • Success Stories
  • Activity
  • Affiliate Program
Company
  • About us
  • Contacts
  • Reviews
  • Press Room
  • CodeGym for EDU
  • FAQ
  • Support
CodeGym CodeGym is an online course for learning Java programming from scratch. This course is a perfect way to master Java for beginners. It contains 1200+ tasks with instant verification and an essential scope of Java fundamentals theory. To help you succeed in education, we’ve implemented a set of motivational features: quizzes, coding projects, content about efficient learning and Java developer’s career.
Follow us
Interface language
Programmers Are Made, Not Born © 2023 CodeGym
MastercardVisa
Programmers Are Made, Not Born © 2023 CodeGym
This website uses cookies to provide you with personalized service. By using this website, you agree to our use of cookies. If you require more details, please read our Terms and Policy.