user2417746
user2417746

Reputation: 11

Searching for null slot in array

I am trying to search for the first null slot in the array. Can you parseInt() quotes to do this, or would I use "stobar[b] == null"?

int[] stobar = new int[100];
for(int b = 0; b < stobar.length; b++)
{
    if(stobar[b] == Integer.parseInt(""))
    {
        stobar[b] = row;
        stobar[b+1] = col;
        break;
    }
}

Upvotes: 1

Views: 382

Answers (2)

Petros Tsialiamanis
Petros Tsialiamanis

Reputation: 2758

You can use

Integer[] stobar = new Integer[100];
...

for(int b=0; b<stobar.length; b++ )
{
    if(stobar[b]==null)
    {
      stobar[b] = row;
      stobar[b+1] = col;
      break;
    }
}

Are you sure that you want to use a static array? Maybe an ArrayList is more suitable for you.

I don't know what are you trying but take a look at the following implementation

public class Point
{
  private int row;
  private int col;

  public Point(int row, int col)
  {
    this.row = row;
    this.col = col;
  }

  public static void main(String[] args)
  {
    List<Point> points = new ArrayList<Point>();

    ...
    Point p = new Point(5,8);
    points.add(p);
    ...
  }

}

Upvotes: 1

Antimony
Antimony

Reputation: 39461

Neither of those are going to work the way you want, since you have a primative array, which can only hold integers. If you want a distinct null value, you need to make it an Integer[] instead.

Upvotes: 8

Related Questions