-2

I'm trying to display the number of elements in an Array that I have made. It is a regular Array with a string as the data type. If it is possible to display the total number of elements in an Array, will the code be similar to an ArrayList's?

    String[] list = new String[14];
    list = new String[] {"Bob", "Sal", "Jimmy" ,"John"};


    list = insert(list, "Joe", 0);
    list = insert(list, "Manny", list.length);
    list = insert(list, "Joey", list.length/2);



    System.out.println(Arrays.toString(list));

}

public static String[] insert(String[] original, String value, int k) {
    String[] copy = new String[original.length + 1];


    for(int i=0; i<k; i++)
    {
        copy[i] = original[i];
    }


    copy[k] = value;


    for(int i=k; i<original.length; i++)
    {
        copy[i+1] = original[i];
    }


    return copy;
}
  • 1
    Total number of elements in an Array: `array.length` – Andreas Sep 16 '17 at 18:01
  • 1
    What is the point of creating an array of 14 elements, just to throw it away and replace it with an array of 4 elements in the next line? – Andreas Sep 16 '17 at 18:06
  • Possible duplicate of [How to find length of a string array?](https://stackoverflow.com/questions/4862092/how-to-find-length-of-a-string-array) – Bernhard Barker Sep 16 '17 at 18:21
  • @Andreas It was part of the assignment to demonstrate that you know how to increase the size of an array – Trush Patel Sep 16 '17 at 18:42

2 Answers2

1

As already mentioned the best Array type to use is a List or ArrayList for inserting elements into that array by way of the ArrayList.add(index, valueToAdd) method. It of course can be done with basic Arrays but a little more work on your part is required to carry out the task as you have already noticed. One possible way might be like this:

public static String[] insertStringArrayElement(String[] original, String value, int atIndex) {
    // Make sure the supplied index (k) is within bounds
    if (atIndex < 0 || atIndex > original.length) {
        throw new IllegalArgumentException("\ninsertArrayElement() Method Error! "
                                    + "The supplied index value is invalid (" 
                                    + atIndex + ")!\n");
    }

    String[] tmp = new String[original.length + 1];
    int elementCount = 0;   //Counter to track original elements.
    for(int i = 0; i < tmp.length; i++) {
        // Are where we want to insert a new elemental value?
        if (i == atIndex) {
            tmp[i] = value; // Insert the new value.
            i++;            //Increment i so to add the orininal element.
            // Make sure we don't go out of bounds.
            if (i < original.length) {
                tmp[i] =  original[elementCount]; // Add original element.
            }
        }
        // No we're not...
        else {
            tmp[i] = original[elementCount]; // Add original element.
        }
        elementCount++; // Increment counter for original element tracking.
    }
    return tmp; // Return our new Array.
} 

As Andreas has so kindly pointed out, in your original posted code you had declared the list String Array and initialized it to contain 14 elements but in the very next line you reinitialize this array to contain only 4 elements. The first initialization is a waste of oxygen, time, memory, effort, etc since you could have just declared and initialized the Array like this:

String[] list = new String[] {"Bob", "Sal", "Jimmy" ,"John"};

or better yet:

String[] list = {"Bob", "Sal", "Jimmy" ,"John"};

Just something to remember.

DevilsHnd - 退職した
  • 8,739
  • 2
  • 19
  • 22
0

I suggest you to use an ArrayList if possible, since it would be a lot easier with the method size(). If you have to use an array, since the array is already sized, you could check with a for loop, which slot of the array is != to null and add them to a counter.

  • 1
    Nothing in the question mentions not counting null values, and the array in the example has no nulls *(ignoring the garbage 14 element array that's immediately thrown away)*. – Andreas Sep 16 '17 at 18:08