This question already exists:
Properly declare 2D array of Ints [duplicate]
Closed 6 years ago.
I have this Java variable in my Table.java file:
int[][] evaluationTable = {{3, 4, 5, 7, 5, 4, 3},
{4, 6, 8, 10, 8, 6, 4},
{5, 8, 11, 13, 11, 8, 5},
{5, 8, 11, 13, 11, 8, 5},
{4, 6, 8, 10, 8, 6, 4},
{3, 4, 5, 7, 5, 4, 3}};
That I want to convert to Objective-C in my Table.m file.
It's important that I steer clear away from NSArray, NSMutableArray, etc...
Objective-C is a superset of C, for your literal array you can just use a C array. In C the array brackets come after the array name, and you must specify the number of columns - specifying the numbers of rows is optional; otherwise the declaration is the same as Java. So just change your code to:
int evaluationTable[][7] = { ...
or
int evaluationTable[6][7] = { ...
For more details look up C arrays and compound literals.
HTH
Related
This question already has answers here:
How do I declare and initialize an array in Java?
(31 answers)
Closed 1 year ago.
I was wondering if there is a way in java to initialize an array by listing its components. I could do it in c# like this:
int[] array = new int[10]{ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
I tried this in java but it doesn't work, is there a way to do something similar?
int[] array = new int[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
int[] arr = {1,2,3,4,5,6,7,8,9,10};
I know there are a lot of questions regarding indices, but none of them solve my problem. I am trying to get a model loaded with diffuse light source into openGl ES and rotate it. I first started with a 3D cube and managed to rotate it. Then i added a light source. That also went without a glitch. Now i'm trying to load a model from it's .obj file and rotate it in place of the cube. I tried two .obj converters but it didn't do the trick for me. Then i created my own .obj importer and the result was the same. I get a distorted image (the lighting and rotation still work on the distorted model). Now i feel that my knowledge about indices might be incomplete.
What I am doing is i am creating a vertexPositionArray[] to store the vertex information and a vertexNormalArray[] to store the corresponding normals with a 1:1 ratio. Now i create an indexArray which stores the order in which the position and the normals should be called.
For eg. if i pass in 3, 5, 6, I am referring to the 3rd, 5th, 6th positions in the vertexPositionArray[] and vertexNormalArray[] both or just the vertexPositionArray?
This is a model that i want to use in my project, and on the sides are the output i am getting
Here is a piece of code that i used to extract the info i wanted from .obj file:
String[] vData={"-1.000000f, 0.000000f, 1.000000f,",
"1.000000f, 0.000000f, 1.000000f,",
"-1.000000f, 0.000000f, -1.000000f,",
"1.000000f, 0.000000f, -1.000000f,",
"0.513184f, 0.000000f, 0.513184f,",
"0.513184f, 0.000000f, -0.513184f,",
"-0.513184f, 0.000000f, -0.513184f,",
"-0.513184f, 0.000000f, 0.513184f,",
"0.158195f, 0.690612f, 0.158195f,",
"0.158195f, 0.690612f, -0.158195f,",
"-0.158195f, 0.690612f, -0.158195f,",
"-0.158195f, 0.690612f, 0.158195f,"};
String[] nData={"-0.889400f, 0.457200f, 0.000000f,",
"0.000000f, -1.000000f, 0.000000f,",
"0.000000f, 1.000000f, 0.000000f,",
"0.889400f, 0.457200f, 0.000000f,",
"0.000000f, 0.457200f, 0.889400f,",
"0.000000f, 0.457200f, -0.889400f,"};
int[] vOrder={8, 12, 11,
6, 5, 3,
7, 6, 1,
8, 7, 2,
5, 8, 4,
11, 12, 9,
6, 10, 9,
5, 9, 12,
7, 11, 10,
7, 8, 11,
1, 6, 3,
2, 7, 1,
4, 8, 2,
3, 5, 4,
10, 11, 9,
5, 6, 9,
8, 5, 12,
6, 7, 10};
int[] nOrder={ 1, 1, 1,
2, 2, 2,
2, 2, 2,
2, 2, 2,
2, 2, 2,
3, 3, 3,
4, 4, 4,
5, 5, 5,
6, 6, 6,
1, 1, 1,
3, 3, 3,
3, 3, 3,
3, 3, 3,
3, 3, 3,
3, 3, 3,
4, 4, 4,
5, 5, 5,
6, 6, 6,
};
FileWriter write = new FileWriter( path , false);
PrintWriter print_line = new PrintWriter( write );
print_line.println("float[] vertexData ={");
for(int i=0;i<vOrder.length;i++)
{
print_line.println(vData[vOrder[i]-1]);
vCount++;
}
print_line.println("};");
print_line.println("float[] vertexNormal ={");
for(int i=0;i<nOrder.length;i++)
{
print_line.println(nData[nOrder[i]-1]);
}
print_line.println("};");
print_line.println("Vertex Count = "+vCount);
print_line.close();
}
}
I got vData (vertex) and nData (normals) from the obj file by using the replace function in a text editor. Similarly i got the vOrder and nOrder by the data on the faces given below (Adding the number before'//' to vOrder and the one after to nOrder):
f 8//1 12//1 11//1
f 6//2 5//2 3//2
f 7//2 6//2 1//2
f 8//2 7//2 2//2
f 5//2 8//2 4//2
f 11//3 12//3 9//3
f 6//4 10//4 9//4
f 5//5 9//5 12//5
f 7//6 11//6 10//6
f 7//1 8//1 11//1
f 1//3 6//3 3//3
f 2//3 7//3 1//3
f 4//3 8//3 2//3
f 3//3 5//3 4//3
f 10//3 11//3 9//3
f 5//4 6//4 9//4
f 8//5 5//5 12//5
f 6//6 7//6 10//6
Could anyone please tell me what is it I'm doing wrong here?
Thanks in advance. Any help would be appreciated.
if i pass in 3, 5, 6, I am referring to the 3rd, 5th, 6th positions in the vertexPositionArray[] and vertexNormalArray[] both or just the vertexPositionArray?
Both
Do i need to bind my position and normal arrays into 1 array?
No, but you can, not required.
tell me if i understand the .obj file correctly:- f 8//1 5//4 6//3 for this i pushed the vertexPositionArray[7] and vertexNormalArray[0] into a new vertexPArray[] and vertexNArray[], respectively.
Yes, because Obj files're not related to OpenGL at all.
In OpenGL, you can have only one indices array but OBJ files, you can have three types of inces (f v1/vt1/vn1 v2/vt2/vn2 v3/vt3/vn3 ....) so you should create your new vertexPArray[] and vertexNArray[] using the faces data.
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)
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I have 2D java array, for example:
int[]][]arr = {{1, 2, 3, 4, 5, 6}, {7, 8, 9, 10, 11, 12}}
I want to copy several left and right columns of this to right and left sides of my array. This my target:
int[][] arr = {{5, 6, 1, 2, 3, 4, 5, 6, 1, 2}, {11, 12, 7, 8, 9, 10, 11, 12, 7, 8}}
In real life my array is very large (1500x600) and I need a fast solution.
I know about System.arraycopy but can not figure out how to use it here.
For the arrays you posted as example:
int[][] arr1 = {{1, 2, 3, 4, 5, 6}, {7, 8, 9, 10, 11, 12}}
int[][] arr2 = new int[2][10];
// ...
for(int n = 0; n < arr1.length; n++) {
System.arrayCopy(arr1[n], 0, arr2[n], 0, 2);
System.arrayCopy(arr1[n], 0, arr2[n], 2, 6);
System.arrayCopy(arr1[n], 4, arr2[n], 6, 2);
}
// here arr2 will have what you want
*Update: Resolved did a deep copy, thanks for the help
I am using a vector of integers to simulate some sorting algorithms, when i insert numbers to the test vector and shuffle the order and pass it to a sorting function, if i pass the void sorting functions the same vector, when the vector gets sorted in a function previously passed the newly sorted vector gets passed to the function following it because it is already sorted i cannot show the sorting process. For example in my following code
#SuppressWarnings("unchecked") // Removes error given at when adding elems to int_vec
public static void CreateVec (int array_len)
{
Vector <Integer> int_vec = new Vector(array_len);
int temp_int = 1;
int low_bound = 0;
int high_bound = array_len - 1;
for(int i = 0; i<array_len; i++)
{
int_vec.addElement(temp_int);// creating a vec in respect to array len
temp_int ++;
}
Collections.shuffle(int_vec);
System.out.println("OG vec: " + int_vec); //original vector (random order)
BubbleSort(int_vec,array_len); //sending int_vec to bubble sort
InsertionSort(int_vec,array_len); // retrieves newly sorted vector sorted from BubbleSort (problem)
}
So my question follows, how can i keep sending my test vector (int_vec) with the randomly ordered elements rather than it keep sending the sorted vector to the other algorithms. Note i correctly implemented these algorithms, it works if i comment out the function calls to the other algorithm functions.
Create a copy of int_vec with new Vector<Integer>(int_vec) and pass in the copy to your sorting methods. This way, only the copy will get sorted, and int_vec will still be randomly ordered, and ready to be copied again for the next sorting method.
And yes, this is a shallow copy, but a deep copy is not needed here.
it doesnt seem to be working i did the following
Vector <Integer> int_vec = new Vector(array_len);
Vector <Integer> copy_bub = new Vector <Integer> (int_vec);
//...//
BubbleSort(int_vec,array_len);
InsertionSort(copy_bub,array_len);
and this is the output
Output:
OG vec: [4, 8, 9, 6, 10, 2, 1, 5, 3, 7]
Copy vec: [4, 8, 9, 6, 10, 2, 1, 5, 3, 7]
Bubble Vec: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
Insertion Vec: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
Insertion Vec: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
Insertion Vec: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
Insertion Vec: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
Insertion Vec: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
Insertion Vec: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
Insertion Vec: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
Insertion Vec: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
Insertion Vec: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
Insertion Vec: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]