Inline Array Definition in Java - java

Sometimes I wish I could do this in Java:
for (int i : {1, 2, 3, 4, 5})
System.out.println(i);
Unfortunately, I have to do something like this instead:
int [] i = {1, 2, 3, 4, 5};
// ...
My recollection is that C++ has this sort of feature. Is there an OOP replacement for inline array definitions (maybe even to the point of instantiating anonymous classes)?

I think the closest you are gonna get is:
for(int i : new int[] {1,2,3,4})

You could create the int[] array in the for loop.
for (int i : new int[] {1, 2, 3, 4, 5}) {
...
}
Here you are making an anonymous int array, which is the closest thing to what you want. You could also loop through a Collection.
Note that this question has nothing to do with OOP. It's merely a matter of syntax. Java supports anonymous arrays/objects just like C++.

You don't have to create a separate variable for this. The syntax {1, 2, ...} is valid only for declarations, but you can always say new int[] {1, 2, ...}:
for (int i : new int[] {1, 2, 3, 4, 5})

Related

What's the advantages in these 2 types of declaration?

I have a question on the styles of declaration of arrays
//I get that you may just want to initliaze array with 0s for some reason
int[] myIntArray = new int[3];
//I get that you already know what values you want in this array
int[] myIntArray = {1, 2, 3};
What's the advantage in declaring in these 2 notations:
//Woulnd't I just use the 1st notation for this
int[] myIntArray;
myIntArray=new int[3];
//Wouldn't I just use the 2nd notation for this
int[] myIntArray = new int[]{1,2,3};
The 4th notation is almost as same as the 2nd notation except the fact that there are 2 references,myIntArray and an anonymous array, to the object {1,2,3} where the 2nd reference,anonymous is lost instantly
The difference between int[] myIntArray = {1, 2, 3}; and int[] myIntArray = new int[]{1,2,3}; is that the first syntax only works when initializing a variable.
So if you have code like this:
int[] myIntArray = {1, 2, 3};
// some code
if (someCondition) {
myIntArray= new int[] {4, 5, 6};
}
You can not replace the second one with just {4, 5, 6}, because that syntax is reserved for initializing only.
But new int[] {4, 5, 6} is a general expression that works basically anywhere.
It's mostly used where you want to construct an array and not assign it to a variable, such as directly passing it to a method call:
someFunctionTakingAnIntArray(new int[] {3, 4, 5});

Java take range from array

Does Java have a method of Array drop? In Scala we have: Array.drop(10).take(16) or maybe to take a range of members of an array?
In Java I can only do array[10] for example.
There is Arrays::copyOfRange:
It has three parameters:
original: the source array
from: the starting index, inclusive
to: the end index, exclusive
Not that it returns a new array, meaning that if you change the values of the resulting array, the original array does not change.
The method is overloaded to work for all primitive types and for objects.
Here's an example use:
import java.util.Arrays;
public class Main {
public static void main(String[] args) {
final int[] source = IntStream.range(0, 10).toArray()
System.out.println(Arrays.toString(source));
final int[] result = Arrays.copyOfRange(source, 3, 8);
System.out.println(Arrays.toString(result));
}
}
Which prints:
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
[3, 4, 5, 6, 7]
For more information, see the docs
I think it's easiest to achieve such semantics by streaming the array:
SomeClass[] sourceArray = /* something */;
SomeClass[] result =
Arrays.stream(sourceArray).skip(10L).limit(16L).toArray(SomeClass[]::new);

Multidimensional array in Scala

Is it possible to create a multidimensional array in scala without using the "Array()"
Like this in java:
int[][] myIntArray = {
{1, 2, 3},
{4, 5, 6},
{7, 8, 9}
};
If i understood correctly, you dont want to declare the array repeating Array a lot of times.
You can try this:
val > = Array
val x: Array[Array[Int]] = >(
>(1, 2, 3),
>(4, 5, 6),
>(7, 8, 9)
)
Source (There are other suggestions also)

Need clarification for java array declaration

If we do int[] b = {2, 4}; then we have an array called b, with length 2. From what I understand, the java compiler is implicity doing int[] b = new int[] {2, 4}; for me, fine.
Similarly, if we do int[] c = new int[2]; , then we get an array called c initialzed to {0, 0} My confusion comes from why the following doesn't work:
Why can't I do int[] d = new int[2] {5, 6};
It's just a compiler thing. Besides, why would you want to manually input the size anyway?
This will just be a source of error.
The int[] b = new int[] {2, 4} notation is for convenience, if you already know the contents of the array that you want to declare.

How to use java.util.Arrays

I'm trying to use the java.util.Arrays class in JavaSE 6 but not sure how i would implement it? on an array that I have generated?
before the start of the class i have
import java.util.Arrays
Java Arrays
To declare an array of integers, you start with:
int[] myArray;
To instantiate an array of ten integers, you can try:
myArray = new int[10];
To set values in that array, try:
myArray[0] = 1; // arrays indices are 0 based in Java
Or at instantiation:
int[] myArray2 = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
To get values from the array, try:
System.out.println(myArray[0]);
To print all the values in an array, try:
// go from 0 to one less than the array length, based on 0 indexing
for(int i = 0; i < myArray2.length; i++) {
System.out.println(myArray2[i]);
}
For more information, the tutorial from Sun/Oracle will be of great help. You can also check out the Java language specification on Arrays.
Using the Arrays Utility Class
java.util.Arrays contains a bunch of static methods. Static methods belong to the class and do not require an instance of the class in order to be called. Instead they are called with the class name as a prefix.
So you can do things like the following:
// print a string representation of an array
int[] myArray = {1, 2, 3, 4};
System.out.println(Arrays.toString(myArray));
Or
// sort a list
int[] unsorted = {3, 4, 1, 2, 5, 7, 6};
Arrays.sort(unsorted);
Well let's say you have an array
int[] myArray = new int[] { 3, 4, 6, 8, 2, 1, 9};
And you want to sort it. You do this:
// assumes you imported at the top
Arrays.sort(myArray);
Here's the whole shebang:
import java.util.Arrays;
class ArrayTest {
public static void main(String[] args) {
int[] myArray = new int[] { 3, 4, 6, 8, 2, 1, 9};
Arrays.sort(myArray);
System.out.println(Arrays.toString(myArray));
}
}
And that results in
C:\Documents and Settings\glow\My Documents>java ArrayTest
[1, 2, 3, 4, 6, 8, 9]
C:\Documents and Settings\glow\My Documents>
You have not provided enough information about what you are trying to do. java.util.Arrays only exposes static methods, so you simply pass in your array and whatever other params are necessary for the particular method you are calling. For instance Arrays.fill(myarray,true) would fill a boolean array with the value true.
Here is the javadoc for java.util.Arrays
You can use a static import
import static java.util.Arrays.*;
int[] ints = {3, 4, 1, 2, 5, 7, 6};
sort(ints);
public static void main(String[] args) {
double array[] = {1.1,2.3,5.6,7.5, 12.2, 44.7,4.25, 2.12};
Arrays.sort(array,1,3);
for(int i =0;i <array.length;i++){
System.out.println(array[i]);
}
}
result:
"1.1,2.3,5.6,7.5,12.2,44.7,4.25,2.12"

Categories