I have been making a basic game of battleship and to get the CPU to choose ship locations on a 2d array I am making two random number generators to pick values from 1 to 8 on a separate method. For some reason, the random generators show up as unused and I don’t know why. so far this is what
Tag: arrays
Object array with variable from another class
Worksheet Question: The question said to declare an array of 5 objects of the class Node in the main Class – I managed to this as shown below But then the question continues populate the array by objects with seqNo values assigned to 1,2,3,4,5 respectively. Then traverse the array and print the list of its object using method show() I
check if first array is sorted and if there are consecutive duplicate elements check second array at the index of duplicate elements
We need to check if the first array is sorted, and if there are consecutive duplicate elements, check the second array at the index of duplicate elements. This is my code and output The output I expect is false since a[2]=a[3] and thus we should move to the 2nd array and there b[2]>b[3]. for ex: 1. this should return true
How to process object with map for CSV output
I have a set of below Objects, which i need to write to CSV: Above set can have a map with two, three or four values. Output of CSV required: I started with below snippet to print out: But above is creating a two column csv with userId and behaviours printing all map object behaviours. How to achieve above type
Cannot invoke ” ” because array is null
I need to add information about 2 actors such as their name, address and age and have done so without arrays easily but it’s necessary, I keep getting the error “Cannot invoke “TestActor.setName(String)” because “actors[0]” is null at TestMain.main(TestMain.java:5)” This is just a test main I’m using to test it ”’ And this is the actor class I’m using that
How to Find Index of a Duplicate in An Array (Java)
For this programming assignment, we are supposed to find the index of a duplicate in this array for one row of a sudoku puzzle. I have this method, static boolean equals(int[] a, int[] a2): This program right here simply prints out this: Duplicate – 9 found at index 0 and 0 which means that no duplicate is found. I commented
How do I convert an Int to an Int[] in java?
I want to convert a primitive Java integer value: To an integer array: Answer There can be so many ways to do it. A concise way of doing it’s using Stream API as shown below: Output: Notes: ASCII value of ‘0’ is 48, that of ‘1’ is 49 and so on. Math#abs returns the absolute value of an int value
Remove Alternate Elements from ArrayList in java 7
I have an array of String containing below elements Now what I want to do I need to remove all the decimal value in string format like 5.0,5.5 and 6.0 so my final array should contain elements 99,100,101 so what I’d done so far is show in below code I have hardcoded the values which is quite bad practice as
Confused on how to work with both indexes and values in an array in Java
The above code is supposed to accept an int position, compare it to the values in the array, and based on the position, convert the value at the given position to Kilometers using the conversion above. I am confused on how to get the position to work with the index of the array instead of just the values directly. I
Sort arrays using lambda
I want revers-sort java8. I don’t want use stream. Here is an example: I want it like this: {5, 4, 3, 2, 1}; So my code is this But I get an error message: Operator ‘-‘ cannot be applied to ‘T’, ‘T’ How can I reverse sort and sort? Is there a better way? Answer You can try if you