scala 2.11.8 how to fill an array - arrays

I want to create an array that contains the same value repeated for a very large number of times, say 1,000,000.
I was thinking to use something like Array.fill(1000000)(0). However, after reading the documentation for Scala 2.11.8, I found that there is no such members of Array in this version.
Is there any other ways that can create the array without using loop? Thanks in advance for your help.

This will do the trick:
Array.fill[Int](1000000)(0)
Read more here: https://alvinalexander.com/scala/scala-list-class-examples

You can use range to iterate through required length (1000000 times in your case) and then return a default value which is 0 in each iteration as below.
val arr:Array[Int] = (1 to 1000000 map(_ => 0)).toArray

Stream.continually(0).take(1000000).toArray would do that .. but why in the world would you want something like this???

Related

Apache Spark / Scala: How to get all the elements of an array except the last one?

I need to get all possible subdomains from the domain name, such as:
www.abc.xyz.com, and I have an array (www.abc.xyz.com,abc.xyz.com,xyz.com,com). Now I want to explode this array into:
www.abc.xyz.com
abc.xyz.com
xyz.com
but I do not want the last element: com
Note that the size of the array can vary.
How can I achieve this? Thanks in advance for all the help!!
Scala has a nice function for this. It is called as dropRight:
arr.dropRight(n)
This will drop all the last "n" elements. It also handles exceptional cases. E.g. if your array length < n then it will simply return empty array.
https://www.scala-lang.org/api/current/scala/Array.html

Find the first "1" in zero array

I have an array "0000011111"
I need to find the first occurrence of "1".
How can I do that in efficient way ?
my solution is: (I think there is a better way)
$array = array(0,0,1,1,1);
for($i=0;$i<count($array);$i++)
{
if($array[$i] == 1)
{
var_dump($i);
return;
}
}
Your solution is already as efficient as possible, but there's a built-in method in PHP that will do this for you:
$array = array(0,0,1,1,1);
var_dump(array_search(1, $array)); // int(2)
Note that array_search will return the boolean FALSE in the case where there are no 1s in the array.
EDIT
I made the assumption that the original code is PHP just because it looked that way. :-)
Unfortunately, since there is no necessity for any of the numbers to be "1" and since you are only going through the array once, this is the most efficient solution. Binary search or any such algorithm wont work as this array is quite obviously not sorted.
Sample inputs:
0101101
1000101
In either case, binary search would not work.
If you can somehow convert the array efficiently to a number, It is possible to find the first 1 efficiently with log base 2.
var number = 0b010000010;
console.log(Math.floor(Math.log2(number)))
EDIT The main reason for doing this is because there are hardware instructions for doing log base 2 that make it constant time.
Of course if you cannot store your array as a binary string, because it is too long or something like that, this solution is not for you.

Swift Similar Element Array

If I have an array with elements linked to xassets with names like "card1", "card2", etc up to "card100" is there a quicker way to write these elements in the array format without having to write out all 100?
Also if this has been answered already please let me know or link to it. About 2 days into trying to teach myself to program.
Not exactly sure what you're asking but you may want to look at loops - Swift Loops. You can add items to an array in a for loop from 1 to 100.
Is this what you are looking for ?
let cards = Array(1...100).map({"card\($0)"})

MATLAB struct array field access

I have an array of structures in MATLAB and I want to plot all of the values for one key. I know you can print it by doing array.key but for some reason hist(array.key) doesn't work.
Seems simple enough but I couldn't find how to do this.
clear all
array(10).key = 1:10;
mat = [array.key]
hist(mat)
Try something like the above.

How to display or convert and integer array in j2me?

The question is quite simple but I can't find the answer.
I'm using j2me.
I have an integer array of 9 elements.
After all the computations now I want to print it or show it in a form.
It is something like Integer.toString(); for use with an array?
I know I can use a loop but I want to know if it's a faster way.
Use a loop but don't use the String + operator. Use StringBuffer.append().

Resources