This question already has answers here:
The best way to print a Java 2D array? [closed]
(14 answers)
Closed 2 years ago.
I am new to Java and was wondering what I did wrong. I am building a simple board game, but for some reason the array prints as an array of "Ljava.lang.String;#4bec1f0c". I've read some similar questions to this problem but for some reason, the solutions that I have found does not work in my code.
Help is appreciated.
for(int row = 0; row < gameBoard.length; row++)
{
for(int col = 0; col < gameBoard[row].length; col++)
{
gameBoard[row][col] = "- ";
}
}
System.out.print(Arrays.toString(gameBoard));
if you want to every object be visible in a 2-dimension array, use this method:
System.out.print(Arrays.deepToString(gameBoard));
Arrays.toString is working in single dimension arrays.
more data and examples:
https://www.geeksforgeeks.org/arrays-deeptostring-in-java-with-example/
Arrays.toString does the right thing for one-dimensional arrays, but won't help much on two-dimensional arrays.
Use Arrays.deepToString instead.
Related
This question already has answers here:
Loop diagonally through two dimensional array
(12 answers)
Closed 1 year ago.
It's giving outOfBounds every time. I have tried every possible alteration now but it's not giving the output. The O/P should be 7-4-8-1-5-9-2-6-3
for(int g=n-1; g>=0; g--) {
for(int i=g, j=0; i>=g; i++, j++) {
System.out.print(a[i][j]);
}
System.out.println();
}
for(int g=0; g<n; g++) {
for(int i=0, j=i+g; j<n; i++,j++) {
System.out.print(a[i][j]) ;
}
System.out.println();
I think the first thing you can do is change the i>=g to a better limit.
As you are incrementing i positively, and i is already equal to g, it will keep going up past the limit of the array's bounds. Maybe i<(the limit of your bounds) should solve it.
If I was trying to iterate through a square matrix, I would use only one variable and iterate with a[i][i] to reduce confusion:
for (i=0; i > (your bounds); i++) {
System.out.println(a[i][i]);
}
This is the base logic but you seem to be trying to achieve something a little more advanced, so modify it as you see fit.
This question already has answers here:
Java JTextArea - Adding a newline
(4 answers)
Closed 7 years ago.
private void displayButtonActionPerformed(java.awt.event.ActionEvent evt) {
for(int j=0; j <= topics.size(); j++) {
outputBox.setText("Pg#"+ j + ": " + topics.get(j));
}
This bit of code will only display one element from my array in the text area, any Ideas or assistance will be greatly appreciated.
Every single time you call setText() you are replacing the data. You can only do one because you keep over writing it.
You need to add a new line instead.
In order to do this you want to:
outputBox.append(yourtext);
outputBox.append('\n'); //when you want to add a new line
This question already has answers here:
How do I print my Java object without getting "SomeType#2f92e0f4"?
(13 answers)
Closed 7 years ago.
I'm trying to create this two dimensional array in java and print the output, but when it outputs I am not getting the numbers or a two-dimensional structure. Java appears to be giving me what looks like memory addresses for each array result.
My code
public static int[][] generateSampleTable(int smallest, int largest, int sampleSize)
{
int sampleTable[][] = new int[sampleSize][2];
for (int i = 0; i < sampleSize; i++)
{
sampleTable[i][0] = 5150;
sampleTable[i][1] = 2738;
}
return sampleTable;
}
public static void main(String[] args){
System.out.println(Arrays.toString(generateSampleTable(1,1,10)));
}
My output appears like this:
[[I#803816, [I#22b4b7, [I#1079ed1, [I#231c5d, [I#178cf47, [I#d65c13, [I#1fca60e, [I#1f99518, [I#fa7e97, [I#90925f]
Ignore the smallest and largest arguments in that method as I haven't implemented the calculations for them yet and am not using them right now.
Other than what you see here, I guess I should tell you that yes, I imported java.util.Arrays
Not sure what is going on. Any help would be appreciated.
Thanks
Arrays.toString() knows how to print 1 dimensional arrays. For 2-dimensional arrays you should use Arrays.deepToString().
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I am trying to create an array of arrays so that i can have an array of years that breaks down into an array of 12 months that then can be broken down into 31 days. I am not sure if this process is doable in java or not, just trying to find a good method. thanks
You have to be way more specific about what you want to do, or what you have done, or any of that. But because you asked about collections of collections
You can use a multi-dimensional array. http://www.java2s.com/Tutorial/Java/0140__Collections/ArrayOfstringArrays.htm
String[][] cartoons = {
{ "Flintstones", "Fred", "Wilma", "Pebbles", "Dino" },
{ "Rubbles", "Barney", "Betty", "Bam Bam" },
{ "Jetsons", "George", "Jane", "Elroy", "Judy", "Rosie", "Astro" },
{ "Scooby Doo Gang", "Scooby Doo", "Shaggy", "Velma", "Fred", "Daphne" } };
Or Check out the API for ArrayList: https://docs.oracle.com/javase/7/docs/api/java/util/ArrayList.html
This is how you would use one though.
ArrayList<String[]> year= new ArrayList<>();
String[] february = new String[28];
year.add(february);
Though you need to be more specific about what you want to do. Because using collections to handle temporal data is a fools errand.
You could simply create a two(or more) dimensional array
int[][] array = new int [5][12];
This would be an array of 5 years, each with 12 months. You could go even further to make a three dimensional array (picture a cube) or even a four dimensional array (I'll let you think about that one :))
See this post: Syntax for creating a two-dimensional array
You can represent a date as an array of arrays like so:
int date [][][] = new int [2015][12][31];
for(int y = 0; y < date.length;y++){
for(int m = 0; m < date[0].length;m++){
for(int d = 0; d < date[0][0].length;d++){
date[y][m][d] = d+1;
}
}
}
But why? To access a specific date, you need the indices, but if you know the indices, you already know the date.
And some of the values of this 'calendar' are invalid, e.g. 11-31,02-30,...
This question already has answers here:
Arrays.asList() not working as it should?
(12 answers)
Closed 9 years ago.
So I'm just learning about arrays in Java (interesting stuff) but I'm having some problems getting my head around the contains() method.
I tried:
Random rn = new Random();
int first = 12;
int[] tab = new int[first];
for (int i = 0; i <= first - 1; i++) {
tab[i] = rn.nextInt(10);
Which seemed to work fine for filling in my Array, but then I tried a:
System.out.println(Arrays.asList(tab).contains(9));
And no matter what, even if I fill the array manually with 9's, it'll still only print up "false".
What am I doing wrong?
Try this
Integer[] tab = new Integer[]{9};
System.out.println(java.util.Arrays.asList(tab).contains(9));
I get
true