0

Just trying to understand the basics of how this should work. Here is my code.---------------------------> This is my main class.

public class Driver
{
    public static void main(String[] args)
    {
        //create new instance of the ArrayLab class with parameter of 10
        ArrayLab array = new ArrayLab(10);

        //search for 2
        array.search(2);
    }
}

The class ArrayLab has a method assigned to it called search with parameter of (2). So far this is what I have.

import java.util.Arrays;
public class ArrayLab
{
    //array instance variable
    int[] array1 = new int[10];

    //array constructor
    public ArrayLab(int integer)
    {
        //class parameter = 10
        int[] array1 = new int[integer];

    }

//method
public void search(int integer)
    {
        int[] array1= new int[]{integer};
        System.out.println(Arrays.toString(array1));
    }
}

So the big question is what am I doing right? or wrong? I realize this is probably pretty basic, just struggling to understand what is happening inside the code. Thanks :)

4
  • do you have a error or do you just want to know whats happening? Commented Oct 22, 2015 at 3:00
  • Just edited. When I try to print it doesn't return the parameter I set in the search method (2). Commented Oct 22, 2015 at 3:03
  • " When I try to print it doesn't return the parameter I set in the search method" , well that's because search has a void return type , read up on return types otherwise you'll be stuck on this forever. Commented Oct 22, 2015 at 3:07
  • Word of advice: come up with more descriptive names for your variables. Forcing yourself to come up with names that reflect the meaning of the variable will help you think through the logic of what you want to achieve. For example, in public ArrayLab(int integer), integer would be better named capacity, since it determines how many elements the ArrayLab can hold. If search is actually supposed to access the element at the specified index of the ArrayLab, then it would be better named get(int index). search suggests you're going to search to see if the ArrayLab contains it. Commented Oct 22, 2015 at 14:33

3 Answers 3

2

Your Driver class is good.

So, lets take one line at a time

int[] array1 = new int[10];

Okay, you made a public int array of size 10, more precisely [0, 0, 0, 0, 0, 0, 0, 0, 0, 0].

public ArrayLab(int integer)
{
    int[] array1 = new int[integer];
}

This is called a constructor. You are passing in integer, and making a new array called array1 which is local to this scope, therefore different than the one before. This array1 contains integer-many zeros.

To use and initialize the previous array1, change your code up to here to this

int[] array1;
public ArrayLab(int integer)
{
    this.array1 = new int[integer];
}

Next,

public void search(int integer)
    {
        int[] array1= new int[]{integer};
    }
}

This, again, creates a new array, but only one value. So say integer was 2, then [2].

Sign up to request clarification or add additional context in comments.

9 Comments

This will define the size of the array [integer], He might get confused. Better to put the value itself.
I should probably change the parameter names to be unique as to not get confused!
@YoungHobbit - not sure which one you are talking about
@YoungHobbit - It doesn't define the size. The size goes in the square braces as before. That line makes an array of size 1, with the only element being the value of integer. I updated the answer to specify that.
I also realized that my instance variable only needs to be "int[] array1;"
|
1

I don't know what the purpose of your ArrayLab class is , but here are some problems

  1. In the constructor you are initializing a local array1 not your instance variable .

  2. search method is doing nothing but again initializing a local array1.

3 Comments

Ah ok, so it should be int[] array1= {integer};
that all depends on what you want to do in your search method .
Ok interesting, So I'm eventually working on building it into something that can search through an array of #'s and see if it matches the parameter.
0

Alright, so whats happening is in your class Driver your creating a object of your class ArrayLab. You send this class a constructor which creates a local variable array1. Your search class initializing another local array1 this is what i would do for your ArrayLab class

import java.util.Arrays;
public class ArrayLab
{
    int[] array1;

    //array constructor
    public ArrayLab(int integer)
    {
        this.array1 = new int[integer];

    }

//method
public void search(int integer)
    {
        System.out.println(array1[integer]);
    }
}

4 Comments

works perfect, even gets rid of the brackets! Even better :)
so far all the answers are good information, so hard to pick! :P
That isn't a search method. It accesses array1 at position integer instead of either returning the index of integer or if array1 contains integer.
ah i thought that we were searching the array by sending a indices that we wanted return, my mistake.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.