58
votes
7answers
185k views

Creating Two-Dimensional Array

int[][] multD = new int[5][]; multD[0] = new int[10]; Is this how you create a two-dimensional array with 5 rows and 10 Columns? Saw this code online. The syntax didn't make sense. So, wanted to ...
20
votes
7answers
133k views

Initialising a multidimensional array in Java

What is the correct way to declare a multidimensional array and assign values to it? This is what I have: int x = 5; int y = 5; String[][] myStringArray = new String [x][y]; myStringArray[0][x] = ...
3
votes
8answers
7k views

sorting 2D array of String in java

I know that this question might have been asked before, but I was not able to find a fit answer. So say I have this array: String [][] theArray = {{"james", "30.0"},{"joyce", "35.0"},{"frank", ...
4
votes
6answers
33k views

How to create a Multidimensional ArrayList in Java?

I'm fairly new to ArrayLists anyway but I need them for this project I'm doing so if you guys could help me I would be more than grateful! Basically, I need to create a multidemensional ArrayList to ...
1
vote
7answers
9k views

How to deep copy an irregular 2D array

How can I deep copy an irregularly shaped 2D array in Java? Ie. int[][] nums = {{5}, {9,4}, {1,7,8}, {8,3,2,10}} I'm unable to use ...
9
votes
3answers
7k views

Java: A two dimensional array is stored in column-major or row-major order? [closed]

In Java, is a multidimensional array stored in column-major or row-major order?
3
votes
5answers
9k views

Arrays constants can only be used in initializers error

public proj 3{ static string [][]Item; public static void main(String [] args){ Item[][] = { {"BH," , "Backhoe," , "200.00"}, {"ER," , "Electric Rake," , "10.00"}, {"EL," , ...
11
votes
1answer
4k views

Maximum number of dimensions in a Java array

Out of curiosity, how many dimensions of an array can you have in Java?
12
votes
4answers
7k views

Java: Multi-dimensional array vs. One-dimensional

For example: a) int [x][y][z] vs b) int[x*y*z] Initially thought i'd go with a) for simplicity I know that Java doesn't store arrays linearly in memory like C does. But what implications does ...
13
votes
6answers
7k views

Is it possible to dynamically build a multi-dimensional array in Java?

Suppose we have the Java code: Object arr = Array.newInstance(Array.class, 5); Would that run? As a further note, what if we were to try something like this: Object arr1 = ...
5
votes
6answers
3k views

Multidimensional arrays in Java and C#

In C# there are 2 ways to create mutlidimensional arrays. int[,] array1 = new int[32,32]; int[][] array2 = new int[32][]; for(int i=0;i<32;i++) array2[i] = new int[32]; I know that the first ...
3
votes
3answers
11k views

Multiple Keys to Single Value Map Java

I think my question is similar to this one: How to implement a Map with multiple keys? but with an important difference. In that question (if my understanding of it is correct, please let me know if ...
2
votes
3answers
2k views

Java HashMap associative multi dimensional array can not create or add elements

Okay so I have spent several hours trying to wrap my head around this concept of a HashMap in Java but am just not able to figure it out. I have looked at many tutorials but none seem to address my ...
1
vote
1answer
3k views

java: How to split a 2d array into two 2d arrays

I'm writing a program to multiply matrices (2d arrays) as efficiently as possible, and for this i need to split my two arrays into two each and send them off to a second program to be multiplied. The ...
15
votes
5answers
16k views

casting Arrays.asList causing exception: java.util.Arrays$ArrayList cannot be cast to java.util.ArrayList

I'm new to Java and am trying to understand why the first code snippet doesn't cause this exception but the second one does. Since a string array is passed into Arrays.asList in both cases, shouldn't ...
5
votes
4answers
4k views

Performance comparison of array of arrays vs multidimensional arrays

When I was using C++ in college, I was told to use multidimensional arrays (hereby MDA) whenever possible, since it exhibits better memory locality since it's allocated in one big chunk. Array of ...
3
votes
4answers
8k views

Sorting a 2 dimensional array

I've got a 2D array that I'd like to sort into descending order depending on the contents of the first column, however I'd like the array to retain each row and move the second column as the first ...
7
votes
7answers
18k views

How do I copy a 2 Dimensional array in Java?

I need to make a copy of a fairly large 2 dimensional array for a project I am working on. I have two 2D arrays: int[][]current; int[][]old; I also have two methods to do the copying. I need to ...
3
votes
4answers
6k views

Convert ArrayList into 2D array containing varying lengths of arrays

So I have: ArrayList<ArrayList<String>> Which contains an x number of ArrayLists which contain another y number of Strings.. To demonstrate: Index 0: String 1 String 2 String 3 ...
2
votes
4answers
112 views

Spiraling through a 2d array (l-r, down, r-l, down, l-r, …)

I'm making a board for snakes and ladders, so far I've got the board printed out in descending order. However, I need the board to be printed out the proper way. EDIT: "Spiraling down" means ...
0
votes
1answer
107 views

How to freely traverse the elements in a two-dimensional array by cardinal direction? (DOWN, UP, LEFT, RIGHT)

This question is about figuring the path through a maze, as is represented by a two-dimensional array. For example, the path through this maze 0 1 2 3 4 --------- 0| 1 0 1 1 1 1| 1 0 0 0 1 2| 1 ...
16
votes
5answers
36k views

how to create dynamic two dimensional array in java?

I want to create a two dimensional array dynamically. I know the number of columns. But the number of rows are being changed dynamically. I tried the array list, but it stores the value in single ...
7
votes
1answer
16k views

Java Comparator class to sort arrays

Say, we have the following 2-dimensional array: int camels[][] = new int[n][2]; How should Java Comparator class be declared to sort the arrays by their first elements in decreasing order using ...
3
votes
1answer
5k views

Create, populate and return 2D String array from native code (JNI/NDK)

I'm finding this particular bit of code quite difficult (Not least of which because I only started playing with C a week ago). I've been trying hard to find the right syntax to correctly create a ...
2
votes
2answers
790 views

Creating an n-dimension Array in Java during runtime

I have a String which contains arrays. Example: "[[1, 2], [4, 5], [7, 8]]" Now, I want to make an actual Java array out of this. I have created a function to get the dimensions and a recursive ...
2
votes
6answers
6k views

Union of 2 arrays in java?

class Union{ //Search Function static boolean search(int A[], int i){ for(int k =0;k<A.length;k++) { if(A[k] == i) return true; } return false; ...
6
votes
1answer
1k views

Arrays with trailing commas inside an array initializer in Java

Array initializers can be used to initialize arrays at compile-time. An initializer with trailing commas as shown below compiles fine. int a[][] = {{1,2,} ,{3,4,} , {5,6,},}; //Trailing commas cause ...
5
votes
5answers
5k views

Java Program using 4D array

I'm a first year computer engineering student and I'm quite new here. I have been learning Java for the past three and a half months, and C++ for six months before that. My knowledge of Java is ...
4
votes
9answers
3k views

2D dynamic array using ArrayList in Java

I need to implement a 2D dynamic array. The number of rows is fixed, say n. But the number of columns for each row is not fixed and equivalent. For instance, the first row has 3 elements and the ...
9
votes
6answers
3k views

find elements surrounding and element in an array

I have a multidimensional array, I want to get the elements surrounding a particular element in that array. For example if I have the following: [[1,2,3,4,5,6] [8,9,7,5,2,6] [1,6,8,7,5,8] ...
2
votes
3answers
6k views

Making a player move on 2D array game grid

I am creating a game using a 10x10 2D array. The player starts at the top left hand corner indicated as "P" and the objective is to get the player to avoid obstacles to get to the treasure indicated ...
2
votes
6answers
271 views

Find similar rows in 3 two-dimensional arrays

What is the best approach to solve the following problem in Java? There are 3 two-dimensional arrays that have equal number of rows and columns: Array1: [1]: {1, 2, 3} [2]: {2, 2, 4} [3]: {1, 1, 1} ...
0
votes
2answers
105 views

GameLogic, x in a row game

I'm making a game and I need to make a method which checks if the specified cell is part of a horizontal consecutive sequence of cells containing the same character. The sequence of cells needs to be ...
0
votes
3answers
287 views

Multidimensional array in java

I have create following string multidimensional array in java. which has top outer array is level(6 levels)and under each level has 4 different sub-level(4 sub levels) and each group has individual 10 ...
0
votes
2answers
3k views

JSON Multi dimensional Array to Java Multi-Dimensional Array

I am trying to convert a JSONArray whose String format is multi-dimensional to a Java multi-dimensional array. I have tried a lot of ways by myself and am getting lost in my task. Hopefully someone ...
9
votes
4answers
33k views

Getting the array length of a 2D array in Java

I need to get the length of a 2D array for both the row and column. I’ve successfully done this, using the following code: public class MyClass { public static void main(String args[]) { ...
5
votes
2answers
17k views

Multidimensional Arrays lengths in Java

I need to find the lengths of a multidimensional array with non equal indices. For example, I have int[][] pathList = new int[6][4] Without actually hard-coding the indices, I need to find the '6' ...
2
votes
3answers
4k views

how to traverse though a 2d char array searching for words in java?

I am having trouble with a school assignment and would really appreciate some insight. I am asked to create a wordsearch using a 25x25 2d char array and somehow go through that array by developing an ...
18
votes
2answers
48k views

Get the size of a 2D array [duplicate]

Okay, so I have a 2D array z[50][50] and z's size is therefore 50 * 50, but if I say z.length I only get 50 back. How do I get the real size of a 2D array?
7
votes
7answers
3k views

java: multidimensional generic array creation

How do I make a multidimensional array of generic items in java? Consider the class: class A<T> { T t; public A(T t) { this.t = t; } } When I try to create a multidimensional ...
7
votes
3answers
3k views

Java N-Dimensional Arrays

I need to be able to have an n-dimensional field where n is based on an input to the constructor. But I'm not even sure if that's possible. Is it?
7
votes
4answers
1k views

Multidimensional arrays in Java extends which class?

I need to know which class multidimensional arrays in Java extends exactly? When we assign Object[] ref=new int[]{1,2,3}; the compiler complains that the objects are of different types. So it ...
5
votes
4answers
2k views

Java equivalent for the Numpy multi-dimensional object

After using it for a while, I really like the Numpy multi-dimensional array. It's helpful to write algorithms with a concise yet readable and fairly general code. I wish to have the same thing in ...
3
votes
3answers
833 views

Flipping a multidimensional array java [closed]

I'm currently working on a Tetris AI, and I as looking for a method to flip a 4 by 4 multidimensional array. I've looked all over, and the most i could find was rotating, which wouldn't work in my ...
10
votes
4answers
40k views

Initialize 2D array

I am trying to initialize a 2D array, in which the type of each element is char. So far, I can only initialize this array in the follow way. public class ticTacToe { private char[][] table; ...
7
votes
8answers
8k views

What's the best way to set all values of a three-dimensional array to zero in Java?

I have a three-dimensional array that I want to reset to zero. It seems that there should be an easy way to do this that doesn't involve three for loops: for (int i = 0; i < n; i++) { for ...
5
votes
4answers
14k views

Matrix multiplication using arrays

I'm trying to make a simple matrix multiplication method using multidimensional arrays ([2][2]). I'm kinda new at this, and I just can't find what it is I'm doing wrong. I'd really appreciate any help ...
5
votes
7answers
3k views

How to sort a two-dimension ArrayList

I have a two-dimension ArrayList that contains double values: ArrayList<ArrayList<Double>> data = new ArrayList<ArrayList<Double>>(); In analogy with classic arrays , I ...
5
votes
4answers
1k views

Set rank of array at runtime

I was wondering what the simplest way would be to implement an array who's rank is specified at runtime. The example I am working on stores a array of boolean values for lattice points, and I want ...
3
votes
3answers
534 views

Java multidimensional array considered a primitive or an object

Is int[][] matrix = new int[10][10]; a primitive or is it considered an object? When i send it as a parameter to a function, does it send its reference (like an object) or its value (like a ...