Can Anyone Explain This Array Declaration to Me

Can anyone explain this array declaration to me?

There's no difference. In Ruby, you're free to add a trailing comma to an array. It makes syntax like this:

a = [
1,
2,
3,
]

A bit nicer, in some cases (e.g., if you want to add an element, you simply add a 4, line and don't have to worry about checking for a comma on the last line).

Can anybody explain array declaration below with pointers

typedef short int(SPTR)[2];

defines SPTR to be an array of two short ints.

SPTR *p, *q;

defines p and q to be pointers to arrays of two short ints. Without the typedefs, you could define them as:

short int (*p)[2];
short int (*q)[2];

p = q = (short int(*)[2])a2D[1];

This is an abuse of the explicit cast facility of the language.

Given the declaration of a2D, the type of a2D[1] is char [3], i.e. an array of 3 chars. When used in an expression like above, it decays to a pointer, char*. Without the explicit cast, the assignments above would fail.

Everything else after that is difficult to make sense of. It comes under the category of undefined behavior.

Array declaration with assigning address

In your second example, ptr is not an array but a pointer to an array.

This pointer is initialized with a which is an array that, in this context, decays to a pointer to its first element. The type of a is int [6][3], i.e. an array of size 6, where each element is an array of type int [3]. So a pointer to an element of a has type int (*)[3], which matches the type of ptr.

What's the differences between Array declaration and Collection declaration

Sedan[] is a sub-type of Car[]. But this has a very negative impact on type safety, because the following code is thus legal:

Sedan[] sedans = new Sedan[10];
Car[] cars = sedans; // legal, since Sedan[] extends Car[]
cars[0] = new Car(); // Houston, we have a problem

This code causes an ArrayStoreException at runtime, because an array supposed to only contain sedans would contain a non-Sedan car.

The goal of generics was to have type-safe collections. And the decision was thus taken to design them so that the above problem would be detected at compile time, rather than runtime. Collections are much more used than arrays, and it's quite a good thing to have them type-safe.

So they decided that a List<Sedan> would not be a List<Car>.

Confused about Swift Array Declarations

First two have the same effect.

  1. declare a variable array1_OfStrings, let it choose the type itself. When it sees [String](), it smartly knows that's type array of string.

  2. You set the variable array2_OfStrings as type array of string, then you say it's empty by []

  3. This is different because you just tell you want array3_OfStrings to be type array of string, but not given it an initial value.

I think the first one is recommended as The Swift Programming Language uses it more often.

How do I declare and initialize an array in Java?

You can either use array declaration or array literal (but only when you declare and affect the variable right away, array literals cannot be used for re-assigning an array).

For primitive types:

int[] myIntArray = new int[3]; // each element of the array is initialised to 0
int[] myIntArray = {1, 2, 3};
int[] myIntArray = new int[]{1, 2, 3};

// Since Java 8. Doc of IntStream: https://docs.oracle.com/javase/8/docs/api/java/util/stream/IntStream.html

int [] myIntArray = IntStream.range(0, 100).toArray(); // From 0 to 99
int [] myIntArray = IntStream.rangeClosed(0, 100).toArray(); // From 0 to 100
int [] myIntArray = IntStream.of(12,25,36,85,28,96,47).toArray(); // The order is preserved.
int [] myIntArray = IntStream.of(12,25,36,85,28,96,47).sorted().toArray(); // Sort

For classes, for example String, it's the same:

String[] myStringArray = new String[3]; // each element is initialised to null
String[] myStringArray = {"a", "b", "c"};
String[] myStringArray = new String[]{"a", "b", "c"};

The third way of initializing is useful when you declare an array first and then initialize it, pass an array as a function argument, or return an array. The explicit type is required.

String[] myStringArray;
myStringArray = new String[]{"a", "b", "c"};

Why do I get a syntax error on array declaration?

Each Java application needs an entry point, so the compiler knows where to begin executing the application. In the case of Java applications, you need to wrap up your code in the main() method.

public class HelloWorld{

public static void main(String []args){
System.out.println("Hello World");
}
}

Your code should be

package sort;

public class InsertionSort {
public static void main (String[] args) {
int[] polje = { -2, 5, -14, 35, 16, 3, 25, -100 };

for(int firstUnsorted = 1; firstUnsorted < polje.length; firstUnsorted++) {
int newElement = polje[firstUnsorted];
int i;
for (i = firstUnsorted; i > 0 && polje[i - 1] > newElement; i--) {

}
}

for (int i = 0; i < polje.length; i++){
int firstUnsorted = 1;
int elemant;

}

}
}


Related Topics



Leave a reply



Submit