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));
Related
This question already has answers here:
error: cannot find symbol array.add(element);
(5 answers)
Closed 4 years ago.
import java.util.Arrays;
class Solution {
public int[] sortedSquares(int[] A) {
int[] b =new int[A.length];
int k = 0;
for(int i:A){
k=i*i;
b.add(k);
}
Arrays.sort(b);
return b;
}
}
This is not the way to add things into array, there is no add method for array, use index for adding values to arrays
for(int i =0; i<A.length; i++){
k=A[i]*A[i]; // you can also use Math.pow()
b[i]=k;
}
Making above clode clear and clean. by using for loop
for(int i =0; i<A.length; i++){
b[i]=A[i]*A[i]; // you can also use Math.pow()
}
And also by using for each
int k = 0;
for(int i:A){
b[k]=i*i;
k++;
}
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:
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:
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:
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 */);
}