Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7671873
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T16:09:25+00:00 2026-05-31T16:09:25+00:00

I just got a homework assignment, and it’s just creating a linear search algorithm

  • 0

I just got a homework assignment, and it’s just creating a linear search algorithm in java. However, I also want to make this code work with any data type like Strings, ints, doubles, etc and different kinds of containers like arrays, hashsets, lists, and collections, so I won’t have to waste so a lot of extra lines to overload the method. Here’s the current code.

public class Search
{
    public int linear (Object[] contents, Object query)
    {
        for (int index = 0; index < contents.length; ++index)
        {
            if (query.equals(contents[index]))
            {
                return index;
            }
         }
         return -1;
    }
}

However, in my other class, for testing, I have an int array and an int to search for. I get an error that says “The method linear(Object[], Object) in the type Search is not applicable for the arguments (int[], int)”

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T16:09:26+00:00Added an answer on May 31, 2026 at 4:09 pm

    int is not an object, it’s a primitive. you may user Integer[]. Better yet, you should use generics. You may also want to use list instead of array (just a thought): I show both below.

    import java.util.List;
    public class LinearSearch<E>
    {
    public int linear (E[] contents, E query)
    {
        for (int index = 0; index < contents.length; ++index)
        {
            if (query.equals(contents[index]))
            {
                return index;
            }
         }
         return -1;
    }
    
    public int linear (List<E> contents, E query)
    {
        for (int index = 0; index < contents.size(); ++index)
        {
            if (query.equals(contents.get(index)))
            {
                return index;
            }
         }
         return -1;
    }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This is for a homework assignment, so I don't want the exact code, but
I got this exercise, is not a homework , I just trying to solve:
I've got what I think is a cipher (this isn't homework, just a challenge
Just got a question about generics, why doesn't this compile when using a generic
Just got my feet wet in sorting algorithm with Haskell. I've implemented insertion-sort and
This is a homework assignment and I'm having trouble with my output. Everything works
I've got a homework assignment where I have a base class Package, and two
hi everyone I'm here again, this week I got this Homework: I should find
For my theory of computing languages class, we got a homework assignment to implement
So first off, this is a homework assignment, so please don't write any code

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.