This question already has answers here:
How to create a Multidimensional ArrayList in Java?
(13 answers)
Closed 4 years ago.
I am looking for a type of mutidimensional-arraylist which can be initialize by an array just like regular 2d arrays. Such as this:
for(int o = 0; o < n; o++) {
for(int i = 0; i < n; i++) {
num[o][i] = sc.nextInt();
}
}
I greatly appreciate your help.
A simple 2d array could be initialized with the help of Arrays.fill(primitive[], primitive), just like this:
final int[][] arr = new int[10][10];
for (int[] brr : arr) {
Arrays.fill(brr, 0);
}
It would generate you a 10x10 arrays with lots of zeros for you.
I found this guy here ND4J, it resembles a lot Numpy. I believe it could be useful to you too. It has a zeros method too =)
Related
This question already has answers here:
copy a 2d array in java
(5 answers)
Closed 4 years ago.
public static String[][] deepCopy(String[][]toclone){
String[][]clone = new String[4][4];
for(int i = 0; i < 4; i++) {
for(int j = 0; j < 4; j++) {
if(toclone[i][j] != null) {
String s = new String(toclone[i][j]);
clone[i][j] = s;
}
}
}
return clone;
}
I need a deep copy and not just a flat one for my programm..clone() does not work for my problem. Thanks already for your tips and advice.This questions wasnt answered before or atleast not with the question linked with an 2d int array deep copy. That does not help me at all.
You can use either of these:
Arrays.copyOf()
System.arraycopy()
Object clone() method
If you are looking for more advanced library with cloning api's you can check
org.apache.commons.lang3 SerializationUtils
This question already has answers here:
What's the simplest way to print a Java array?
(37 answers)
Closed 6 years ago.
So I have an array and trying to print what was input to the scanner. I'm trying to print the matrix that was input. Heres the code, what am I doing wrong here? I tried to print just graph, doesn't work.
/** Accept number of vertices **/
System.out.println("Enter number of vertices\n");
int V = input.nextInt();
/** get graph **/
System.out.println("\nEnter matrix\n");
int[][] graph = new int[V][V];
for (int i = 0; i < V; i++)
for (int j = 0; j < V; j++)
graph[i][j] = input.nextInt();
System.out.print(graph);
Printing arrays by simply passing it into System.out.print will print the array's hashcode. See this.
What you want to do is: System.out.println(Arrays.deepToString(graph));
Arrays.toString(...) will work for single dimensional arrays.
Arrays.deepToString(...) will work for more complex arrays.
Because when you try to print graph, you are actually printing a reference to an array object (If I am correct).
Also, for variables (like "V") you should use small caps
Instead of printing a graph, do this:
for (int i = 0; i < V; i++) {
for (int j = 0; j < V; j++) {
System.out.print(graph[i][j]);
}
System.out.println();
}
You can print the contents of an array using the Arrays.toString() method but it doesn't work for multidimensional arrays, so for a 2 dimensional array, you'll need to loop through the elements in the first dimension. Like this:
for (int[] g : graph) {
System.out.println(Arrays.toString(g));
}
This question already has answers here:
What's the simplest way to print a Java array?
(37 answers)
Closed 7 years ago.
so basically this is my code:
Random randomgenerator = new Random();
int[] arr = new int[8];
for (int i = 0; i < arr.length; i++) {
arr[i] = randomgenerator.nextInt(100);
}
System.out.println(arr);
}
}
and this is what appears in the console :
[I#106d69c
I really need help with this, I am probably doing some terrible mistake because I am new to Java coding.
You are printing the whole array, so the default toString method of the array gets called (and it prints a hashcode and the type of the array, nothing useful for you).
What you want is something like :
for (int i = 0; i < arr.length; i++) {
System.out.println(arr[i]);
}
Or better yet as #azurefrog said :
System.out.println(java.util.Arrays.toString(arr));
This question already has answers here:
How can I concatenate two arrays in Java?
(66 answers)
Closed 7 years ago.
I am using a function that returns some integers
int[] return;
This function is inside a loop like this
public static int[] toEOBArray(double[] tempVal)
{
int[] out;
for (int i = 0; i < 10; i++)
{
out = fixArray(tempVal[i]);
}
return out;
}
What I want is as new arrays come from fixArray to add them to the previous results, so in the end I have a big array that will contain all the small arrays resulting from fixArray
What is the most efficient way of doing this?
My main problem is not knowing how to initialize the array that is to hold all the values.
If you want to work only with arrays, you must first find the length of the concatenated array. Then you can use System.arraycopy to copy the small arrays to the output array.
public static int[] toEOBArray(double[] in)
{
int[][] arrays = new int[10][];
int len = 0;
for (int i = 0; i < 10; i++)
{
arrays[i] = fixArray(tempVal[i]);
len += arrays[i].length;
}
int[] out = new int[len];
int offset = 0;
for (int i = 0; i < 10; i++)
{
System.arraycopy(arrays[i],0,out,offset,arrays[i].length);
offset += arrays[i].length;
}
return out;
}
If you insist on working with native arrays (as opposed to a Collection like ArrayList) then you will want to use ArrayUtils class from Apache Common Lang that adds many Collection-like features to Java native arrays, one of which is addAll:
ArrayUtils.addAll(fixArray, tempVal);
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Creating a variable name using a String value
Let's say that I want to do a for loop that creates 10 integers named num1, num2, num3....etc.
How can I do this? I cannot seem to find a way to use a predefined string as the name of an object.
I don't think there is any way to do what you are asking. Based on what I think you are trying to accomplish, I think you should use an array or a list:
int [] num = new int[10];
for(int i = 0; i < 10; ++i){
num[i] = // something
}
List<Integer> num = new ArrayList<Integer>();
for(int i = 0; i < 10; ++i){
num.add(/* something */);
}