More efficient way of doing this? (java exercise)

  • Context: Comp Sci 
  • Thread starter Thread starter Arnoldjavs3
  • Start date Start date
  • Tags Tags
    Exercise
Click For Summary

Discussion Overview

The discussion revolves around a Java programming exercise that requires participants to return a new array containing the first elements of two given integer arrays, with specific conditions regarding their lengths. The focus is on exploring potential improvements to the initial solution provided, particularly regarding efficiency and array manipulation in Java.

Discussion Character

  • Homework-related
  • Technical explanation
  • Debate/contested

Main Points Raised

  • One participant presents a solution that checks the lengths of both arrays to determine how to construct the new array.
  • Another participant questions whether Java allows for dynamic resizing of arrays, suggesting an alternative approach that involves initializing an array and conditionally adding elements based on the lengths of the input arrays.
  • A later reply clarifies that the previous question about resizing was not a hint but rather an inquiry, noting differences in how various programming languages handle array sizes.
  • Some participants express that using multiple if statements feels trivial and seek a more challenging approach to the problem.

Areas of Agreement / Disagreement

Participants do not reach a consensus on the best approach to the problem. There are differing views on the efficiency of the proposed solutions and the handling of array sizes in Java.

Contextual Notes

Participants discuss the limitations of Java arrays being of fixed length, which affects how solutions can be structured. There is also an acknowledgment of varying capabilities in different programming languages regarding array manipulation.

Arnoldjavs3
Messages
191
Reaction score
3

Homework Statement


Given 2 int arrays, a and b, of any length, return a new array with the first element of each array. If either array is length 0, ignore that array.

front11([1, 2, 3], [7, 9, 8]) → [1, 7]
front11([1], [2]) → [1, 2]
front11([1, 7], []) → [1]

Homework Equations

The Attempt at a Solution


Java:
public int[] front11(int[] a, int[] b) {
  if (a.length>=1 && b.length>=1) {
    return new int[] {a[0], b[0]};
  }
  else if(b.length==0 && a.length>0) {
    return new int[] {a[0]};
  }
  else if(b.length>1 && a.length==0) {
    return new int[] {b[0]};
  }
  return new int[] {};
}

Is there a more efficient way of doing this without having to check the length of both arrays?
 
Physics news on Phys.org
Can java increase the size of arrays easily? You could initialize an array, then add a[0] to it if a.length>0, then add b[0] to it if b.length>0.
 
mfb said:
Can java increase the size of arrays easily? You could initialize an array, then add a[0] to it if a.length>0, then add b[0] to it if b.length>0.
Is the bolded a hint ? Since arrays are of fixed lengths. I was just thinking as I was doing these challenges that writing a bunch of if statements felt a little too trivial and not challenging...
 
Arnoldjavs3 said:
Is the bolded a hint ?
No, it was a question. I never worked with Java. I know some languages can do that easily (e.g. PHP, JS and python), while in some others it is not advisable (e.g. C++) or completely impossible.

If an array cannot change size, your solution should be the easiest option.
 

Similar threads

  • · Replies 5 ·
Replies
5
Views
3K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 18 ·
Replies
18
Views
3K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 17 ·
Replies
17
Views
3K
  • · Replies 5 ·
Replies
5
Views
2K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 1 ·
Replies
1
Views
1K
  • · Replies 4 ·
Replies
4
Views
3K