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 8382867
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T17:01:33+00:00 2026-06-09T17:01:33+00:00

so i am trying to create an arraySet from only an array and array

  • 0

so i am trying to create an arraySet from only an array and array methods. I have the following piece of code that should find the index of the item chosen (i’ve included comments so you can understand what each method is supposed to do). As you can see the add method calls on the findIndex method before it can add the item. The problem i am having is that the findIndex method brings up an error (missing return value). How would i return just the int index of the item that the code finds? (the question marks in the code are just to show where i am stuck)

/** Find the index of an element in the dataarray, or -1 if not present
 *  Assumes that the item is not null 
 */
private int findIndex(Object item) {

    for(int i=0; i<data.length;i++){
        if(data[i] == item){
            return i;
        }

    }
  return ???
}


/** Add the specified element to this set (if it is not a duplicate of an element
 *  already in the set).
 *  Will not add the null value (throws an IllegalArgumentException in this case)
 *  Return true if the collection changes, and false if it did not change.
 */
public boolean add(E item) {


    if(item == null){
        throw new IllegalArgumentException();
    }
    else if(contains(item) == true){
        throw new IndexOutOfBoundsException();
    }
    else{

        int index = findIndex(item);
        if(index<0||index>count){
            throw new IndexOutOfBoundsException(); 
       }
        ensureCapacity();
        for(int i=count; i>index; i--){
            data[i]=data[i-1];
        }
        data[index] = item;
        count++;
        return true;
    }
}
  • 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-06-09T17:01:35+00:00Added an answer on June 9, 2026 at 5:01 pm

    If I’m understanding your question right it should just be this, the ‘return i’ accomplishes what you are asking, the return -1 is for the not found case:

    private int findIndex(Object item) {
    
      for(int i=0; i<data.length;i++){
        if(data[i] == item){
            return i;
        }
      }
      return -1;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying create a data.frame from which to create a graph. I have
I am trying create a layout. There are several inner layouts that should like
I'm trying create new object from a module class in VBA, and I have
Trying to create Database as follows: USE Master GO IF NOT EXISTS(SELECT [Name] FROM
hi I'm trying create chat using node.js I see example in http://chat.nodejs.org/ I have
I'm trying create a box in my Django app that displays text (and possibly
I'm trying create a ASMX webservice that can perform a HTTP GET request. I
I'm trying create a scaling layout for an Android application. The whole screen should
I trying create a class derivated from System.Web.UI.Page and in override Render i set
I am trying create a small web application that allows a user to login

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.