Is the values in a capacity declared arrayList null? - arrays

String[] arr = new String[10];
This creates an array with 10 elements all set to null. The first null element can be accessed with arr[0]
ArrayList<String> arr2 = new ArrayList<String>[10];
This creates an array with capacity of 10 but apparently no elements. Is this why you cannot use arr2.get(0) which returns an error? Because there are no elements present?

An ArrayList is a class and an object to the class should be created using a constructor
ArrayList<String>[10] produces an error: cannot create array with '<>' error
Using constructor, the ArrayList is constructed as ArrayList<String>(10);
It is to be noted that this statement creates an Empty ArrayList of initial size 10.
The ArrayList only gets populated when you push values into it.
Refer the following for more understanding:
[1] [2] [3]

Related

Typescript create null array of specified length

I am using splice to add elements to an array at specified index.However In order to to do so I have to create a null array to add the elements at particular index.
If I use an empty array,the elements are not being pushed at specific instance.Right now i'm creating an empty array and then pushing null to that array.I want to know if I can achieve this with any other way.
This is what I'm doing:
arr:any[];
for(let i=0;i<userDefinedLength;i++)
{
arr.push(null);
}
You can use arr = new Array(userDefinedLength).fill(null);
Use fill:
arr: any[] = new Array(userDefinedLength).fill(null);
You can't use null[] unless you're just using the array as a placeholder:
arr: null[] = new Array(userDefinedLength).fill(null);

How to store a reference to an array element?

I want to store a reference to an array element and modify the array element using the reference.
Example Code:
var myArray : [String] = ["foo"]
var element = myArray.first!
element.append("bar")
print(myArray.first!)
Expected Output:
> foobar
Actual Output:
> foo
My expectation was that first would return a reference to the array element. Instead, Swift returns a copy of the element, meaning the array element doesn't get modified.
Is there a way to store a reference to an array element using Swift arrays?
Swift's String is a value type, so it returns a copy not a reference, if you want to get a reference you should use NSString not String.

What trouble could bring assining reversed() to a swift array?

I'm wondering about the reversed() method on a swift Array:
var items = ["a", "b", "c"]
items = items.reversed()
the signature of the reversed method from the Apple doc says that it returns a
ReversedRandomAccessCollection<Array<Element>>
could that be assigned back to items without doing what the apple doc say which is
For example, to get the reversed version of an array, initialize a new Array instance from the result of this reversed() method.
or would it give problem in the future? (since the compiler doesn't complain)
There are 3 overloads of reversed() for an Array in Swift 3:
Treating the Array as a RandomAccessCollection,func reversed() -> ReversedRandomAccessCollection<Self> (O(1))
Treating the Array as a BidirectionalCollection,func reversed() -> ReversedCollection<Self> (O(1))
Treating the Array as a Sequence,func reversed() -> [Self.Iterator.Element] (O(n))
By default, reversed() pick the RandomAccessCollection's overload and return a ReversedRandomAccessCollection. However, when you write
items = items.reversed()
you are forcing the RHS to return a type convertible to the LHS ([String]). Thus, only the 3rd overload that returns an array will be chosen.
That overload will copy the whole sequence (thus O(n)), so there is no problem overwriting the original array.
Instead of items = items.reversed(), which creates a copy of the array, reverse that and copy it back, you could reach the same effect using the mutating function items.reverse(), which does the reversion in-place without copying the array twice.

Scala Array declaration - unintuitive result for apply(0)

How is this possible? I have not created a new array...yet m(0) has a value of 10.
AND, m(1) is an ArrayIndexOutOfBounds exception...
val m = Array[Int](10) means an array of type Int with one element 10 bound the variable m. m(n) means the n-th element of m.
Thats why m(1) gives you a ArrayIndexOutOfBounds, m has only one element.
Are you mixing it up with the odd Java syntax for arrays? int[] m = new int[10]; Which is a 10 uninitialized elements array.
Array[Int](10) creates an array with one element, 10. Check it here
Still, in Scala you shouldn't access array elements directly without be aware of exceptions. I would prefer something like:
scala> val array = Array(10)
array: Array[Int] = Array(10)
scala> array.drop(5).headOption
res0: Option[Int] = None
to access the 5th element for instance

Swift 2: Writing to plist with multidimensional objects

I am a newbie in Swift and I have been trying something for a long time and I am having an compile error that could not overcome with.
I am trying to write to a plist containing multidimensional array objects.
I need to add an array to the inner array of plist.
The plist is like as follows:
I am trying to populate the most inner array of the plist which is as follows:
I am trying to add ITEM 2 under the ITEM 5.
I am using this code:
notesArray.objectAtIndex(0).objectAtIndex(5).addObject("AA","BB","CC","DD")
Compiler gives me following error :
Cannot call value of non-function type '((AnyObject) -> Void)!'
How can I populate the array inside the parent array directly from the code?
Due to value semantics of Swift arrays you have to reassign all changes to their parent objects
This is the initial array
var array : [AnyObject] = [["OZEN PIZZA", "PIZZA", "15", "20", "tariffoto1", [["Biber","2", "Adet", "11"]]]]
get the root array at index 0 of the array
var rootArray = array[0] as! [AnyObject]
get the array at index 5 of rootArray
var item5Array = rootArray[5] as! [[String]]
append the item
item5Array.append(["AA","BB","CC","DD"])
reassign item5Array to index 5 of rootArray
rootArray[5] = item5Array
reassign rootArray to index 0 of the array
array[0] = rootArray

Resources