I have a set of 2D arrays, structured as so: int[][] cage = new int[someLength][4]. Each array has a set column length of 4; the first two columns are all that concern us with this problem. They are used to define the row and height respectively of a point (e.g. 2,4).
What I need is to generate all possible permutations of the points using both row and column coordinates from the 2D array. I found a working method to generate all permutations for 1D array of ints here: My link, under update 2 of the first answer.
Here is an example of a possible input and corresponding output desired:
I have a set of 2D arrays, structured as so: int[][] cage = new int[someLength][4]. Each array has a set column length of 4; the first two columns are all that concern us with this problem. They are used to define the row and height respectively of a point (e.g. 2,4).
What I need is to generate all possible permutations of the points using both row and column coordinates from the 2D array. I found a working method to generate all permutations for 1D array of ints here: [URL] ..... , under update 2 of the first answer.
Here is an example of a possible input and corresponding output desired:
In: (1,0) (1,1)
Out:(1,0),(1,1) (1,1),(1,0)
Ideally the result will be stored in a 2D array of ints.
I have 5 numbers between 0 to 13 in ascending order stored in an array. They may have repeats. I want to perform permutations and figure out the number of running pairs (minimum 3 numbers in a pair. Ex- 012, 345, 456, 1234, 78910) that I can make with these command line inputs that I get.
Lets say my user input was 01123. For this sequence I'll have the following pairs-
012 012 1234 1234 123 123 0123 0123
There are same repeated sequences because there are two 1's hence same pairs using different 1's.I have the input in an array so I'm wondering if there is any inbuilt class for arrays that would let me do this. If not, whether I can pass it into a collection and then find its permutations. Following which I traverse through the sequences and update a count for each running pair generated to pass onto an array in another variable to perform some other functionality. If this is viable how to implement this in code ? I'm new to java and am not familiar with the classes available.
how the code is getting all the permutations of a string with recursion. The following code works correctly but I am having trouble grasping what it is doing.
public class Main { private static void permutation(String prefix, String str){ int n = str.length(); if (n == 0) System.out.println(prefix); else { for (int i = 0; i < n; i++) permutation(prefix + str.charAt(i), str.substring(0, i) + str.substring(i+1)); } } public static void main(String[] args) { permutation("", "ABCD"); } }
I see that each character is being appended to prefix through each iteration. So I know it adds "A" then "B" etc etc. I can follow it up until the "D" is appended to the prefix string. After that I don't understand how the D moves back to the str string and then the C moves back to str followed by the D moving back to the prefix string. I have stepped through the code many times in my debugger but still don't see how it moves back to str and knows to send D to prefix and not to send C. I feel I don't understand how recursion behaves therefore I can't follow.
I've been pondering about this algorithm for about a week but I'm still not able to write a "fast" working method/algorithm to solve the Number-of-paths-exercise we were given in my class />
So here's the task: Write an efficient java program "Paths" which solves the following task:
- Read input n ∈ N and give output a(n) which is the number of paths from (0,0) to (n,0) it is not allowed to go over the diagonal (m,m) and also not below the x-axis (m,0)
Here are the allowed steps: u = (1,1), U = (1,4), d = (1,−1), D = (1,−4) and H = (1,0) steps are performed in a two-dimensional-coordinate-system!
Ok Im trying to create a code right now that will take a table of rectangular coordinates and convert them to Polar coordinates in the constructor. I will eventually calculate the total distance between all points in the calculateDistancePolar method but for now I am using it to test.
It doesnt like line 29 and 31 of the class Polar and I cannot figure out why.
public class Test { public static void main(String[] args) { double[][] coords = {{86.92, 2.47},{70.93, 27.81},{97.74, 34.36},{30.90, 35.14},{51.66, 31.70},{0.830, 21.77},{55.91, 66.62},{32.92, 75.23},{65.26, 72.53},{83.90, 4.710}}; System.out.println("X,Y Coordinates are:"); outputArray(coords); Polar myTest = new Polar(coords);
I'm having a random x and y coordinate generate and I need to check if the coordinates are inside of a right triangle on this imaginary coordinate plane.
This program deals with a variation of the geographic coordinate system with Greenwich at 51° 28' 38" N. The program will be adding the three values of one coordinate to the three values of the second coordinate. Though the maximum value for seconds and minutes is 59, the user can enter values greater than that number when prompted. The maximum degree is 360 for this assignment though the user can enter values greater than that number.
So I need to generate 10 random integers in the range 1-20 but i have to store them in an array called numbers. Then I have to call a method called displayArray which displays the contents of the array and for the assignment i have to use a for loop to traverse the array. The method header for the displayArray method is:
public static void displayArray(int[] array)
This is what I have done
public class RandomIntegers { static int numbers = 0; public static void displayArray(int[] array) { System.out.println(numbers + "Numbers Generated");
I am taking the Class Algorithms and Datastructures and got an assignment for Lab that really throws me off. The goal is to create an Array out of a given CSV file, implement several Methods that get the size of array, etc.
I am still stuck in the first part where the CSV has to be imported into the Array. My problem is that I need a mechanism that figures out the needed size for the Array, creates the array, and only then transfers the data from the CSV.
The list consists of the following wifi related values:
MAC-Adress, SSID, Timestamp, Signalstrength.
These are on the list, separated by comma. The Columns are each of these, and the rows are the four types of values making up the information on a certain wifi network.
The catch is, we are not allowed to use any of the following:
java.util.ArrayList java.util.Arrays and any class out of java.util.Collection.
So far I used the BufferedReader to read in the file and tried to implement the array, but I get an arrayindexoutofboundsexception.
Below is my Code (Its still an active construction zone):
public class WhatsThere { public WhatsThere(String wifiscan) throws IOException { } public static void main(String[] args) throws IOException { // WhatsThere Liste = new WhatsThere(String wifiscan); String[][] arrayListe = new String[0][0];
I am working on a problem where i have to create a 2d array with given input of the dimensions (odd number) of array, along with a number within the array and to then print out all of the numbers surrounding that number.
Anyway, i am working on simply making the spiral, which should look like the one below.
n = 3
7 8 9 6 1 2 5 4 3
where the 1 always starts in the center with the 2 going to the right, 3 down, then left etc. etc. I was able to create the code by starting on the outer edges rather than the center and working my way to the middle, however my code always starts from the top left and goes around to the center where it needs to start from the top right. I am having trouble altering my code to meet this criteria. This is what i have thus far.
import java.io.*; public class Spiral { public static void main(String args[])throws IOException { BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); System.out.print("Enter the number of elements : "); int n=Integer.parseInt(br.readLine());
We were given a class lab that asks us to write a program that create a multidimensional array ( 5 x 5 ), populates the array using nested loops with letter from A until Y, and displays the array to the screen. and the result should look like this:
A B C D E F G H I J K L M N O P Q R S T U V W X Y
How to write this program.. I have tried all my best but the results are not coming like this..
I have the following code in which I am looping through the rows of one array (composed of Strings) and copying it to another array. I am using .clone() to achieve this and it seems work as it changes the memory location of the rows themselves. I did notice that the String objects are still pointing to the same location in memory in both arrays but I won't worry about that for now, at the moment I just want to understand why the array I am cloning is not successfully assigning to the other array.
This is the incorrect line: ar[r] = maze[r].clone();
Write a method that returns a new array by eliminating the duplicate values in the array using the following method header: public static int[] eliminateDuplicates(int[] list). The thing is that I found the working solution that is written below, but how it works. How to eliminateDuplicates method done with flag and flag2.
Here is the code:
Java Code:
import java.util.Scanner; public class Exercise06_15 { public static void main(String[] args) { java.util.Scanner input = new java.util.Scanner(System.in); int[] numbers = new int[10]; System.out.println("Enter ten numbers: ");
I am receiving an ArrayIndexOutOfBoundsException for the following code, which moves a creature through a 2D array maze. I have altered the clauses of the first if statement for the four direct methods (north, south, east, and west) multiple times (i.e. x + 1 >= 0 && x > 0 && x - 1 > 0 && x < array.length...etc). However, while the code occasionally runs, more often than that it returns this exception. Catching the exception seems like a poor workaround though if worst comes to worst I'll do that.
I included only the relevant functions of the code:
public boolean goNorth(char[][] array) { boolean success = true;; x = getX(); //x = this.x; y = getY(); //y = this.y; if ((x - 1 >= 0 && x - 1 < array.length) && (y >= 0 && y < array[x].length)) {
I am passing input from the user to a method that will initialize an array of the data (scores in this case). The method is filling the entire array with the last input value.
array initializer method
Java Code:
public static float[] inputAllScores(float validScore) { float[] diverScores = new float[7]; for (int i = 0; i < diverScores.length; i++) { diverScores[i] = validScore; } return diverScores; } mh_sh_highlight_all('java');
So I have this stack. I'm writing out all the operations and what not but I'm having trouble bypassing this "generic array creation" problem. I'm meant to be creating an array based implementation of a stack and from my research from google and my various attempts at things, I have not found a solution that works.
In addition; I have all the operations written that I need except for one final one. And that is clear(). clear() is meant to empty the array, essentially it is a popAll() method. Then all I need to do is set up so I can print out the arrays and I should be able to handle everything else.
StackInterface:
/** An interface for the ADT stack. */ public interface StackInterface<T> { /** Adds a new entry to the top of this stack. @param newEntry an object to be added to the stack */ public void push(T newEntry);
/** Removes and returns this stackÕs top entry. @return either the object at the top of the stack or, if the stack is empty before the operation, null */ public T pop();
filling out a Random array: An Array of Specific Length Filled with Random Numbers This time what I need to do is take the elements from this Random array and assign them to a new Byte array:
for(int i = 0; i < limit-10; i++) { Random dice = new Random(); int randomIndex = dice.nextInt(array.length); if (array[randomIndex] < 128) { System.out.print(array[randomIndex] + " "); } else if (array[randomIndex] >= 128) { System.out.print(array[i] + " "); } } byte[] noteValues = new byte[]
{ 64, 69, 72, 71, 64, 71, 74, 72, 76, 68, 76 }; //This is the byte array filled manually!
I've tried amending the manual input to fit in with the Random array, as follows:
byte[] noteValues = new byte[] { array[randomIndex] };
In this case, however, the Byte array can't interpret the int values. Also, if the Byte array is outside the 'for' loop, array[randomIndex] cannot be resolved.
I have double checked this code over and over and I just can't find the problem.
What I'm trying to do is take a file and input it into an 2D array.
Ultimately, I should convert the array of integers to an array of characters, then print it out. The file contains a set of ASCII values.
After printing it out, I should then create methods to manipulate the image produced.
Using 2D arrays is a requirement for this exercise.
I think that somehow I'm overcomplicating this and the solution is a lot more simple than I think, but I can't think of what to change.
The error I am getting is:
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 40 at main.main(main.java:17)
Java Code:
import java.util.*; import java.io.*; public class main { public static void main(String[] args) throws FileNotFoundException { String[][] data = new String[22][40];