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
Related
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.
This question already has answers here:
NullPointerException when Creating an Array of objects [duplicate]
(9 answers)
Closed 2 years ago.
I'm using an Array of ArrayLists of Integer in Java but it can't run. Every time I get the message
"Note: Goal.java uses unchecked or unsafe operations." and
"Note: Recompile with -Xlint:unchecked for details." after compile and when I want to run it message "Exception in thread "main" java.lang.NullPointerException" appears.
the code is (All is in main method) :
int v = 8;
ArrayList<Integer>[] adj = new ArrayList[11];
adj[0].add(21);
and the error belongs to the last line.
searched a lot but nothing ! plz help, stuck for hours now.
According to the comment section, people are right about the null elements in your array of arraylists. To resolve the NullPointerException you should initialize elements of the array right after its declaration and instantiation.
Here's the modified code below that works:
ArrayList<Integer>[] adj = new ArrayList[11];
for (int i = 0; i < 11; i++)
adj[i] = new ArrayList<Integer>();
adj[0].add(21);
You should first initialize all indices of the array:
int v = 8;
ArrayList<Integer>[] adj = new ArrayList[11];
for(int i=0; i<adj.length; i++) {
adj[i] = new ArrayList<>();
}
adj[0].add(21);
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().
This question already has answers here:
Java: Different outputs when add/remove short and integer elements in a Set
(3 answers)
Closed 8 years ago.
Set<Short> set = new HashSet<Short>();
short i = 0;
for (i = 0; i < 100; i++) {
set.add(i);
set.remove(i-1);
}
System.out.println(set.size());
When we run above program output comes 100.
I understood it that As we are inserting short values in set and trying to remove integer value. So It's not removing from set.
But if we modify remove statement as below
set.remove(i);
Output coming 0. Any idea?
While you are removing it from the set, i-1 is treated as object and need to be typecast to shore to remove correct value from the set. set.remove((short)(i - 1))
Set<Short> set = new HashSet<Short>();
short i = 0;
for (i = 0; i < 100; i++) {
set.add(i);
set.remove((short)(i - 1));
}
System.out.println(set.size());
Try this code its giving output as 1.
This question already has answers here:
Randomly select an item from a list
(5 answers)
Closed 8 years ago.
Alright, I keep trying to oogle this but It keeps showing answers with a string array which I can't use. This is currently my code:
List<String> Shuffle = new ArrayList<String>();
if(EAmount == finalk){
Shuffle.add("Emerald");
}
if(DAmount == finalk){
Shuffle.add("Diamond");
}
if(GAmount ==finalk){
Shuffle.add("Gold");
}
if(IAmount == finalk){
Shuffle.add("Iron");
}
I can't find out how to get a random string from Shuffle. Please help!
BTW: The Amounts and finalk are integers
You can generate a random integer between zero and the size of the collection (exclusive), and then use it as an index to refer to the corresponding item of the collection.
int randIndex = new Random().nextInt(Shuffle.size()); //generate rand int [0, size[
String randString = Shuffle.get(randIndex); //get random string
It is the same as with Arrays, you just need to use the get Method:
Shuffle.get(random)