-1

This seems so simple but I'm stuck. I'm getting the usual Index Out Of Bounds Swift errors. It seems Java can set the amount of indexes an array has from the start and increment the total number of indexes as well.

I know what the issue is but I don't know the Swift equivalent of this Java function. The Java function has a post incrementor for increasing the index count of the EMPTY array. I don't know how to write that in Swift. With Swift you have to use append. You can not use subscripting on an empty array. PLUS I don't know how to increase the index count.

How do I convert this Java to Swift?

Java

private int[] theArray;
private int itemsInArray = 0;

public void addItemToArray(int newItem) {
        theArray[itemsInArray++] = newItem;
    }

Swift

var theArray = [Int]()
var itemsInArray = 0

func addItemArray(newItem: Int) {
    theArray[itemsInArray] += newItem
}
addItemArray(newItem: 5)
4
  • But with Swift you have to use append – Doesn't that already answer your question? Commented Mar 2, 2019 at 9:21
  • Is there any reason to be using itemsInArray? You could just use theArray.append(newItem) and then simply use theArray.count to get the number of available elements. A Swift Array is more like a List then a Java array Commented Mar 2, 2019 at 9:22
  • "Java can set the amount of indexes an array has from the start and increment the total number of indexes as well" - Not sure that makes sense. A Java array is fixed length, what the code is doing is moving the index to the next location and then applying the value to that element, but since it's not guarding against the fact that itemsInArray could be higher then the available elements (ie length) it could generate a IndexOutOfBoundsException as well...and the Swift code isn't even close to doing the same thing Commented Mar 2, 2019 at 9:24
  • @TokyoToo I added a bit of explanation to the answer. Commented Mar 2, 2019 at 9:35

1 Answer 1

1

Per documentation, to initialize an Array with a default size use

var theArray = Array(repeating: "", count: itemsInArray) // Where repeating is the contained type 

You can then insert via

theArray.insert(newItem, at: yourIndex)

Array(s) in Java must have an initial size, which cannot be changed after creation. However Swift has types comparable to Java Collection<T> types, which can have variable size.

For example

private int[] theArray;

will compile, but it will also produce a NullPointerException on the first access, since it isn't properly initialized

private int[] theArray = { 1, 2, 3, 4 };
private int[] theArray = new int[10];

You would also need to be careful, both in Java and Swing, to access the correct index range, using the myArray[index] notation in Java, or the myArray.insert(item, at: index) notation in Swing.


The Java line theArray[itemsInArray++] = newItem of your example means

  1. assign the newItem value to the itemsInArray index
  2. increment itemsInArray (see post-increment operator)

In Swift you can just append a new element to the Array, you don't even need to maintain an index like itemsInArray

var theArray = ["One", "Two", "Three"]
theArray.append("Four")

var theIntegerArray = [1, 2, 3]
theIntegerArray.append(4)

Or with an empty array

var theIntegerArray: Array<Int> = []
theIntegerArray.append(4)

Yes, you can use repeating with Integer values. Just

Array(repeating: 0, count: itemsInArray)
Sign up to request clarification or add additional context in comments.

4 Comments

@TokyoToo see updated answer. Just ask if you need clarifications
@TokyoToo then the first four lines of my answer will suffice. Just create a new Array with a default initial size, and use insert. This would be the most similar version to Java
@TokyoToo yes, here you're starting from an empty array, but you're not maintaining an index. This is the same example I used in my answer, and it's not comparable to the Java one.
@TokyoToo see the "Or with an empty array" part.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.