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
Related
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 =)
This question already has answers here:
Scanner error with nextInt() [duplicate]
(1 answer)
NoSuchElementException with Java.Util.Scanner
(10 answers)
Closed 5 years ago.
i looked around a bit here on the site but found nothing that seemed to help me on this problem.
so here's my little issue. lets say i have a function that looks like this
public static void main(String args[])
{
int[][] array = new int[9][9];
createArray(array);
}
and i'm trying to pass array into this method so that i can initialize it with input that was read from the console.
public static void createArray(int[][] array)
{
Scanner input = new Scanner(System.in);
int i = 0;
int j = 0;
for(i = 0; i < 9; i++)
{
for(j = 0; j < 9; j++)
{
array[i][j] = input.nextInt();
}
}
input.close();
}
i would think that this would work, since java passes arrays by reference so that means that the createArray() method is receiving the memory address of the array back in main, and therefore any changes here affects the original one in main.
but for some reason that i'm not seeing, running this gives me these errors, and i dont understand what i'm doing wrong
This question already has answers here:
What is a NullPointerException, and how do I fix it?
(12 answers)
Closed 6 years ago.
I have a 2d array of "#" and "." I am Passing into a method, I am then trying to iterate through the array and find how many "#" are in it. I keep getting a NULLPOINTER exception when using .equals. the method should return the amount of "#" in the array. I tried iterating through the 2d Array and then converted the 2d into a single dimension array and still am getting the EXCEPTION Im not sure what I am doing wrong, any help would be greatly appreciated.
my code is:
public static int countSeats(String[][] aud){
int openSeats = 0;
String [] audit;
audit = new String[120];
int k = 0;
for(int i= 0; i < aud.length; i++)
for(int j = 0; j < aud[i].length; j++)
audit[k++] = aud[i][j];
for(int i= 0; i <= audit.length; i++){
openSeats = audit[i].equals("#")? +1:+0;
}
return openSeats;
You're getting an exception because of your loop condition:
for(int i= 0; i <= audit.length; i++){
Notice how you're using <= and not <. You're looping through the entire audit array, and then going one more than you should, resulting in the NullPointerException.
The fact that you declare String[] audit, makes the solution even more complicated as you MAYBE TRY TO PUT ALL THE 2-D in 1-D thinking that it will simplify the problem for you, but it wont. Stay inside the 2-D, and do the search
int openSeats = 0;
for(int i= 0; i < aud.length; i++)
{
for(int j = 0; j < aud[i].length; j++)
{
if( aud[i][j].equals("#") )
{
openSeats++;
}
}
}
return openSeats;
As others have pointed out, you are running your for loop one iteration extra by having i <= audit.length; instead of i < audit.length;. Also, as a good practice, always keep the String literal on the left side of the method call. E.g. instead of aud[i][j].equals("#"), do "#".equals(aud[i][j]) to avoid any possibility of NPE.
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);