# CodingBat: Java. Array-1, Part I

For further help with Coding Bat (Java), please check out my books. I am also available for tutoring.

The Array-1 section of CodingBat contains 27 basic array exercises, most of which are very simple. In the following three posts I’ll present my solutions. Just like it was the case with the String-1 section, there isn’t much to comment on.

All solutions were successfully tested on 27 January 2013.

firstLast6:

```public boolean firstLast6(int[] nums) {
return nums == 6 || nums[nums.length - 1] == 6;
}
```

sameFirstLast:

```public boolean sameFirstLast(int[] nums) {
return nums.length >= 1 && nums == nums[nums.length - 1];
}
```

makePi:

```public int[] makePi() {
int[] pie = { 3, 1, 4 };
return pie;
}
```

commonEnd:

```public boolean commonEnd(int[] a, int[] b) {
return a == b || a[a.length - 1] == b[b.length - 1];
}
```

sum3:

```public int sum3(int[] nums) {
return nums + nums + nums;
}
```

rotateLeft3:

```public int[] rotateLeft3(int[] nums) {
int[] rLeft = { nums, nums, nums };
return rLeft;
}
```

reverse3:

```public int[] reverse3(int[] nums) {
int[] newOrder = { nums, nums, nums };
return newOrder;
}
```

maxEnd3:

```public int[] maxEnd3(int[] nums) {
if (nums > nums) {
nums = nums;
nums = nums;
} else {
nums = nums;
nums = nums;
}
return nums;
}
```

sum2:

```public int sum2(int[] nums) {
if (nums.length == 0)
return 0;
if (nums.length == 1)
return nums;
return nums + nums;
}
```

middleWay:

```public int[] middleWay(int[] a, int[] b) {
int[] c = { a, b };
return c;
}
```

For further help with Coding Bat (Java), please check out my books. I am also available for tutoring.

## 8 thoughts on “CodingBat: Java. Array-1, Part I”

1. mayhs

This should also work perfectly fine:

int maxi= Math.max(nums,nums);

nums=maxi;nums=maxi;nums=maxi;
return nums;

2. mayhs

middleWay:

int na[]=new int;
na=a;
na=b;
return na;

1. Gregor Ulm Post author

This does work, too, but what’s the point of your much more verbose code? I don’t see why you wouldn’t want to initialize the array with the correct values, which is what I’ve done in my solution above.

3. Frank Shepherd

What does int mean?

1. Gregor Ulm Post author

“int” is one of the primitive data types in the Java. It stands for “integer”, and the range of an “int” is [-2^31, (2^31) – 1].

4. John Doe

I feel a little dumb right now.
In the first exercise why does it need to be:
nums[nums.length-1]
why not just:
nums.length-1

1. John Doe

Ok i just realized.
I am an idiot

1. john doe

yeah you are an idiot

This site uses Akismet to reduce spam. Learn how your comment data is processed.