How to create array in array
How to create array in array
How to create and initialize an array with another array?
To create and initialize an array with another array I currently do this:
Is there a shorter or more idiomatic way of doing this in C#?
It will be great if this can be done in a single statement, like in C++:
If this cannot be done in a single statement, I will take what I get 🙂
6 Answers 6
Trending sort
Trending sort is based off of the default sorting method — by highest score — but it boosts votes that have happened recently, helping to surface more up-to-date answers.
It falls back to sorting by highest score if no posts are trending.
Switch to Trending sort
I like using LINQ for this:
That being said, Array.Copy does have better performance, if this will be used in a tight loop, etc:
It will be great if this can be done in a single statement, like in C++:
The C# version of this would be:
Use Array.Copy to copy an array
Clone() and ToArray() are syntactically nice because you don’t need to pre-allocate a destination array, but in terms of performance, Array.Copy() is the fastest method (see caveat below).
The reason for Array.Copy() being so fast is that it doesn’t allocate any memory. However, if you require your arrays to be copied to a new region of memory each time, then Array.Copy() is no longer the fastest method.
Here are my performance results:
And here’s the code I used:
Also try the default Clone() function which is implemented from the IClonable interface.
You can achieve this easily by creating new empty array or you can use the given array.
This solution is by no way the fastest nor the most elegant, but if you need to either skip some of the elements from the original array, or perhaps transform them you could do this with Linq
Admitted, that looks a bit stupid, unless you need to perform some kind of operation during copying
Not the answer you’re looking for? Browse other questions tagged c# arrays or ask your own question.
Linked
Related
Hot Network Questions
Subscribe to RSS
To subscribe to this RSS feed, copy and paste this URL into your RSS reader.
By clicking “Accept all cookies”, you agree Stack Exchange can store cookies on your device and disclose information in accordance with our Cookie Policy.
Create array from existed array
I have an array
I want to create 4 new array like:
firs element of myArray1 should be number from myArrays first element(1) to 4 but except 1 itself. So its gonna be <2,3,4>. How can I create this arrays from myArray lets say every time I click button in Java (Android Studio)?
4 Answers 4
Trending sort
Trending sort is based off of the default sorting method — by highest score — but it boosts votes that have happened recently, helping to surface more up-to-date answers.
It falls back to sorting by highest score if no posts are trending.
Switch to Trending sort
Maybe this can be a solution:
You pass an array as parameter and become a List of all created Lists
A simple nested loop should do it. Not the most optimal approach if the lists get very big, but should be simple enough to understand what is happening. Basically, for each element in list we create a new list. And then we add all the other elements to the list.
A simple method that could achieve your goal could look like this:
The code now includes a complete example with usage.
The indexing uses a ternary operator. Basically, before we reach the removeInd index, we copy all the elements to their original locations. After we skip the element to be removed, we need to decrement the original index of the elements by one (this way, we «patch» the hole in the array left by the removed element). This also ensures that we do not index outside the new array.
Example of the indexing in the call of subArray(<1, 2, 3, 4>, 1) :
Hopefully, this clarifies the inner workings of the code a little.
How to create sub array from array
I have large data structure to manipulate and I have some problems sorting similar data into sub array.
I wanna sort like this.
3 Answers 3
Trending sort
Trending sort is based off of the default sorting method — by highest score — but it boosts votes that have happened recently, helping to surface more up-to-date answers.
It falls back to sorting by highest score if no posts are trending.
Switch to Trending sort
Array#reduce the array into an array of arrays by using a object as a hash:
The easiest way to do this is with a dictionary (which JS objects naturally act as).
At this point, you have a set of group arrays, each containing every instance of a particular date. You can iterate over it with Object.getOwnPropertyNames and put each array into the larger array OR you could just use it directly.
try to divide the code to sub problems-
this a code I’ve written for a differend purpose and modified it for the explanation. there might be more effient ways to write it, i’m still learning..
How to create a sub array from another array in Java?
How to create a sub-array from another array? Is there a method that takes the indexes from the first array such as:
I don’t want to go over making loops and making my program suffer.
I keep getting error:
cannot find symbol method copyOfRange(int[],int,int)
This is my code:
8 Answers 8
Trending sort
Trending sort is based off of the default sorting method — by highest score — but it boosts votes that have happened recently, helping to surface more up-to-date answers.
It falls back to sorting by highest score if no posts are trending.
Switch to Trending sort
JDK > 1.5
Arrays.copyOfRange(..) was added in Java 1.6. So perhaps you don’t have the latest version. If it’s not possible to upgrade, look at System.arraycopy(..)
It’s still going to perform a loop somewhere though, unless this can get optimized into something like REP STOSW by the JIT (in which case the loop is inside the CPU).
JDK >= 1.8
I agree with all the answers above. There is also a nice way with Java 8 Streams:
The benefit about this is, it can be useful for many different types of «src» array and helps to improve writing pipeline operations on the stream.
Not particular about this question, but for example, if the source array was double[] and we wanted to take average() of the sub-array:
Using Apache ArrayUtils downloadable at this link you can easy use the method
«boolean» is only an example, there are methods for all primitives java types
How to create a sub array from another array in Java?
How to create a sub-array from another array? Is there a method that takes the indexes from the first array such as:
I don’t want to go over making loops and making my program suffer.
I keep getting error:
cannot find symbol method copyOfRange(int[],int,int)
This is my code:
8 Answers 8
Trending sort
Trending sort is based off of the default sorting method — by highest score — but it boosts votes that have happened recently, helping to surface more up-to-date answers.
It falls back to sorting by highest score if no posts are trending.
Switch to Trending sort
JDK > 1.5
Arrays.copyOfRange(..) was added in Java 1.6. So perhaps you don’t have the latest version. If it’s not possible to upgrade, look at System.arraycopy(..)
It’s still going to perform a loop somewhere though, unless this can get optimized into something like REP STOSW by the JIT (in which case the loop is inside the CPU).
JDK >= 1.8
I agree with all the answers above. There is also a nice way with Java 8 Streams:
The benefit about this is, it can be useful for many different types of «src» array and helps to improve writing pipeline operations on the stream.
Not particular about this question, but for example, if the source array was double[] and we wanted to take average() of the sub-array:
Using Apache ArrayUtils downloadable at this link you can easy use the method
«boolean» is only an example, there are methods for all primitives java types
Источники информации:
- http://stackoverflow.com/questions/49191026/create-array-from-existed-array
- http://stackoverflow.com/questions/41189799/how-to-create-sub-array-from-array
- http://stackoverflow.com/questions/4439595/how-to-create-a-sub-array-from-another-array-in-java
- http://stackoverflow.com/questions/4439595/how-to-create-a-sub-array-from-another-array-in-java/53381282