Assign a value at index out of bound - arrays

I have an array and I first check if the array has the index of given number. I like to assign a value at that index if the array has not got any.
var newArray:Array = [0,1,2,3];//length is 4
if(newArray[5] == "")//true
{
newArray[5] = 5;
}
Adobe Help page says;
Inserting array elements
...
If the Array or Vector doesn’t already have an element at that index, the index is created and the value is stored there. If a value exists at that index, the new value replaces the existing one.
But I am not sure it is about null elements or undefined.
How can I assign a value to index that doesn't exist?
I can push till the given index but wondering is anything else possible.

array reference its elements as not typed so if an element does not exist it can be only undefined (default value for untyped) so in your case no need to check for null or "", you only need to check for undefined.
if(newArray[5] == undefined)
{
newArray[5] = 5;
}
EDIT:
undefined is a keyword in as3 that defines a default value for untyped objects. It is the value you use to check if an untyped object has no current value. (As opposed to a typed object that has null as default value with the exception of numbers).
Using "" as you suggest doesn't work since it is a valid String value and would only work to check if a String object is not null and has a length of 0. Equivalent of String.length == 0.
The assignment is correct, even though the index 4 does not exist at this point the Array Object does not complain and assign the value to index 5.

Related

Kotlin. How to reset each array element to zero?

I'm using FloatArray:
private val values = FloatArray(5)
At some point, I need to reset each array value to zero. I tried doing it like this:
values.onEachIndexed { index, value -> value[index] = 0.0f }
But I am getting this error
No set method providing array access
At the same time, this code works (outside onEachIndexed) and I can set the value for the element:
values[1] = 4.0f
What am I doing wrong ? Please help me
Your attempt does not work because value there represents an element of the array, not the array itself, so you cannot use [] to set it. Reassigning it a new value will not work either, because value is a lambda parameter.
There is a builtin method for filling the entirety (or a portion) the array - fill
values.fill(0.0f)

What is the default value of an element of an uninitialized character array?

here's a code I wrote
int main(){
char arr[50][*];
arr[0][0]=1;
if(arr[0][1]){
printf("%d",arr[0][0]);}
If I am putting 1 as *, there is no output.
but anything greater than 1 in the array size would result in 1 output.that means when I am declaring array size the elements are occupied by some value.
now, my actual need is to write a condition if loop (example)
if(arr[0][1]!='null') // or '0',false,undefined, etc
but I am confused what is there in that empty but declared element, because the above is not working.
There is no default value.
For an object defined inside a function without the static keyword, the initial value is garbage.
You can't test an object to determine whether it's been initialized or not. You have to write your code to avoid reading any object's value before it's been set.
doing(while declaring),
char arr[50][2]={0} //or whatever the size of 2nd dimension other than 2
this will replace all elements(100 in this case) with 0.
So, according to my necessity,I can do
if(arr[0][1] != 0)
for checking if that element is undefined by me.
this example works when your input doesn't contain 0, if it does replcae all elements with something else and also the condition too

How to initial BooleanArray by specific size

I have read Kotlin doc
<init>(size: Int, init: (Int) -> Boolean)
Creates a new array of the specified size, where each element is calculated by calling the specified init function.
The function init is called for each array element sequentially starting from the first one. It should return the value for an array element given its index.
Common
JVM
JS
Native
(size: Int)
Creates a new array of the specified size, with all elements initialized to false.
Constructor Creates a new array of the specified size, with all elements initialized to false.
<init>(size: Int)
Creates a new array of the specified size, with all elements initialized to false.
Constructor Creates a new array of the specified size, with all elements initialized to false.
also try simple code
var booleanArray = <init>(30)
it still not working. any help?
In the documentation, <init> refers to a constructor. You should replace it with the name of the class, which is how you call a constructor. In this case:
var booleanArray = BooleanArray(30)
var booleanArray = BooleanArray(candies.size)
This is how I init dynamic array
Tenfour04's answered your question about <init>, but just to be clear: wherever it talks about "the function init" it's talking about the init parameter in one of the constructors. This is a function which takes an Int (which will be an index in the array) and returns a Boolean value (which will be the value at that index in the array).
So you can make this extremely very useful array:
val oddNumbers = Boolean(100) { it % 2 == 1 }
which will pass in index 0, do 0 % 2 and get 0, which isn't equal to 1, so index 0 is set to false, then the same for index 1 which ends up true, and so on.
Or you can ignore the index and just set the same value for everything:
val allTrue = Boolean(100) { true }
All the array constructors work like this, so you can easily set a default value or fill each index with different values

Duplicate Int in Array , Dictionary or Set in SWIFT

Reading up on Sets and Arrays I find that a Set cannot, or is not able to store duplicate values ( Ints, Strings, etc ).
Knowing this, if we are to solve for finding a duplicate Int in an array and one method is to convert the Array to a Set, how come we don't get an error once the Array is a Set?
The methods below simply return a Bool value if the array contains duplicates.
import UIKit
func containsDuplicatesDictionary(a: [Int]) -> Bool {
var aDict = [Int : Int]()
for value in a {
if let count = aDict[value] {
aDict[value] = count + 1
return true
} else {
aDict[value] = 1
}
}
return false
}
containsDuplicatesDictionary(a: [1,2,2,4,5])
func containsDuplicatesSet(a: [Int]) -> Bool {
return Set(a).count != a.count
}
containsDuplicatesSet(a: [1,2,2,4])
The first function, containsDuplicatesDictionary, I convert the array to a Dictionary, of course this takes a for loop as well. The Set method can be done in one line, which is really nice. But I guess since I am new to this, I would think converting the array would throw an error immediately since theres duplicate values.
What am I missing when it's converted
Thank you.
Set, by design is an unordered, unique collection of elements. The implementation of Set takes care of duplicate values itself, when you try to add a duplicate value, it checks whether the value is already present in the Set or not and if it is, the value is not added.
When you call the initializer of Set that takes a sequence as its input parameter (this is what you use when writing Set(a), where a is of type [Int], under the hood, the initializer adds the elements one by one checking whether any of the new elements are already present in the Set or not.
You could make a custom initializer method for Set that would throw an error if you would try to add a duplicate value to it, but it wouldn't really have any advantages for any users of Swift, hence the current implementation that just doesn't add the value if it is already present in the Set and doesn't throw an error. This way, you can safely and easily get rid of any duplicates in a non-unique collection of elements (such as an array).

C programming: Delete specific value stored in Array

Let us consider an array (say, int a[25];).
Later with the help of some loop I start storing the user input in this array.
At some point of time if user choose to delete the value he just entered from the array.
How can I do that for the user of my program.
I can make that 0, but 0 is also a value he could enter; so I simply want to make it NULL or with some garbage value, as it was when I initialized the array.
You can't delete an element from an array. In C arrays are stored as a contiguous block of memory, so you can't just remove an element. You can use any of the following options:
Use some unused value like -1 to mark the element as deleted.
Use separate array to keep track of deleted elements.
Use linked list to store your elements(Recommend option).
You can't.
If you define
int a[25];
then a consists of 25 int elements. Each element, once you assign a value to it, retains that value until it's reassigned, or until the array ceases to exist. You can't "delete" a value from an array. There is no special NULL value for integers as there is for pointers.
You can pick a special value that denotes an "empty" element (perhaps INT_MIN) -- but then you won't be able to store that value as data. Or you can use another data structure, perhaps an array of bool, to keep track of whether the current value of each element of a is valid or not.

Resources