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 :)
searchhas a void return type , read up on return types otherwise you'll be stuck on this forever.public ArrayLab(int integer),integerwould be better namedcapacity, since it determines how many elements theArrayLabcan hold. Ifsearchis actually supposed to access the element at the specified index of theArrayLab, then it would be better namedget(int index).searchsuggests you're going to search to see if theArrayLabcontains it.