expand java 2d array at left and right [closed] - java

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

Related

Is there a way to initialize an array in java by listing its components? [duplicate]

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};

How does comparator works internally?

It may sound trivial for you but I am having a hard time visualizing the comparator / array.sort. How can we sort a full array using only 2 arguments? How does it work internally?
for example- Input -[5,3,2,6,8,10,1], Output- [1,2,3,5,6,8,10]
Which algo does it use internally? Which 2 objects does it compare at first? (5 compared to 3?) and then what are the next two objects? (5 compared to 2?) or (3 compared to 2)?
public static void main(String[] args) {
Integer[] tring = new Integer[]{5,3,2,6,8,10,1};
lol(tring);
for(int i=0;i<tring.length;i++){
System.out.println(tring[i]);
}
}
public static void lol(Integer[] args) {
Arrays.sort(args,(h1,h2)->h1-h2);
}
You can visualize the process like this.
Integer[] tring = new Integer[] {5, 3, 2, 6, 8, 10, 1};
Comparator<Integer> comparator = (a, b) -> {
System.out.println(Arrays.toString(tring) + " comparing " + a + " and " + b);
return a.compareTo(b);
};
Arrays.sort(tring, comparator);
System.out.println(Arrays.toString(tring));
result:
[5, 3, 2, 6, 8, 10, 1] comparing 3 and 5
[5, 3, 2, 6, 8, 10, 1] comparing 2 and 3
[5, 3, 2, 6, 8, 10, 1] comparing 6 and 2
[2, 3, 5, 6, 8, 10, 1] comparing 6 and 3
[2, 3, 5, 6, 8, 10, 1] comparing 6 and 5
[2, 3, 5, 6, 8, 10, 1] comparing 8 and 5
[2, 3, 5, 6, 8, 10, 1] comparing 8 and 6
[2, 3, 5, 6, 8, 10, 1] comparing 10 and 5
[2, 3, 5, 6, 8, 10, 1] comparing 10 and 8
[2, 3, 5, 6, 8, 10, 1] comparing 1 and 6
[2, 3, 5, 6, 8, 10, 1] comparing 1 and 3
[2, 3, 5, 6, 8, 10, 1] comparing 1 and 2
[1, 2, 3, 5, 6, 8, 10]
The comparator uses a sort called a TimSort
I personally do not feel qualified to explain the timsort algorithm, but I'm sure you can find plenty of explanations on google.
For the second part of your question, the way the comparator uses your two augments is to determine what order any two given values order should be.
So, for example, say if you wanted to sort [6,4] the comparator would use your function a-b and would then plug in the numbers 6 and 4 and get the 2 and because 2 is positive the sort knows that 6 needs to be behind 4. Which would result in [4,6].

Converting Java 2D array into an Objective-C 2D array [duplicate]

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

Understanding indices in OpenGL ES 2.0

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.

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)

Categories