This question already has answers here :
You haven't really told us what you are trying to do here, but I suspect you are mixing up objects, which use {}
and have property access like outerArray.middleArray1
, and arrays which are defined with []
and accessed by index: outerArray[0]
.
You example sort of works because assignments return the assigned value. This means something like:
middleArray1 = [
innerArray1 = ['data'],
innerArray2 = ['more data']
]
is the same as:
middleArray1 = [['data'],['more data']]
with the side effect that innerArray1
and innerArray2
get defined as global variables. For example:
outerArray = [
middleArray1 = [
innerArray1 = ['data'],
innerArray2 = ['more data']
],
middleArray2 = [
' ...more stuff...'
]
];
// log global variable innerArray1 (probably not what you want)
console.log(innerArray1)
// access ith index:
console.log(outerArray[0][0])
Javanotes 8.1, Section 7.5 -- Two-dimensional Arrays, How do you find the length of a 2d array? (then you will have to create each Board() objects inside your array, I left that to you as an exercise) And in this code: chessboard.fill(); You are calling a method of Board. You can only do that on a Board object, not on an array. If you want to call this method on each board object in your array, you have to do:
Continuing on what deadman
said.
Let's break down your multi-dimensional array into levels.
middleArray
is Level 1
middleArray -> innerArray1
is Level 2
middleArray -> innerArray1 -> value
is Level 3
To access data
for example, you will have 3 levels meaning 3 []
brackets.
Example:
var data = outerArray[0][0][0]
will output
data
.
var moreData = outArray[0][0][1]
will output more data
.
outerArray[0]
accesses the first array on Level 1
which is middleArray1
.
outerArray[1]
accesses the second array on Level 1
which is middleArray2
.
outerArray[0][0]
accesses the first array on Level 2
which is innerArray1
.
and so on.
Read over
Creating a Multidimensional Array
2D Array Length, of a column, we pinpoint the row before using . The Array Object. If you've performed any kind of programming before, you might be familiar with arrays. In JavaScript, an array is another object you use to hold values. Instead of assigning and calling properties, an array lets you call values using an index. An index describes the location of a value stored in memory.
Your declaration is a bit strange as noted by TiiJ7. Typically an array of arrays would look more like this:
const outerArray = [
[
[data],
[moredata]
],
[
// more stuff
]
]
And you would call each item by its index like outerArray[0][0][0]
It looks like what you really want is an object.
const dataObj = {
middleObj1: {
innerObj1: [data],
innerObj2: [moredata]
},
middleObj2: {
// more stuff
}
}
This one can be called by key instead of index alone. That is, you can either choose to use dataObj['middleObj1']['innerObj1'][0] or dataObj.middleObj1.innerObj1[0]
Notice how I'm still ending it with an index in both cases. That's because I kept your innermost array in my example. You can nest arrays within objects. In fact arrays ARE a special type of object in JavaScript. JavaScript just uses generic objects instead of multi-dimensional Arrays.
Python: storing object in 2d array and calling its method, Rewriting your code so that it simply runs as a single file: #file containing pieces classes class Piece(object): name = "piece" value = 0 grid_name = "____" class The Revisionist > Software Engineering > Java > Step by Step Java Tutorials > 6.2 Java | Processing 2D Arrays & Passing 2D Arrays to Methods. Table of Contents [ hide] 1 Processing Two Dimensional Array. 1.1 1. Initializing arrays values by User Input. 1.2 2. Initializing arrays with random values. 1.3 3. Printing arrays.
First of all, that's very weird way of creating nested arrays as you may not be able to access array elements with array names like 'middleArray1'
, I tried that but it doesn't work. After all, the array will be available in this format only...
outerArray = [
[
['data'],
['more data']
],
[
...more stuff...
]
];
You could still access inner elements as outerArray[0][0]
(innerArray1) or also
outerArray[0][0][0]
(1st element of innerArray1)
[PDF] Two-Dimensional Arrays, Each element in the 2D array must by the same type,. • either a primitive type or object type. • Subscripted variables can be use just like a variable: rating[0][3] [code]class Student { private String name; private int roll; public void setName(String name) { this.name=name; } public void setRoll(int roll) { this.roll=roll; } public String getName() { return name; } public int getRoll()
Two-Dimensional Arrays \ Processing.org, A two-dimensional array is really nothing more than an array of arrays (a 2D Array of objects Cell[][] grid; // Number of columns and rows in the grid int cols The JavaScript Array class is a global object that is used in the construction of arrays; which are high-level, list-like objects. Description. Arrays are list-like objects whose prototype has methods to perform traversal and mutation operations. Neither the length of a JavaScript array nor the types of its elements are fixed.
Arrays of objects | Think Java, Chapter 12 Arrays of objects. During the Figure 12.1: Memory diagram of an array of strings. When we display a card, println automatically calls toString . Arrays are Objects. Arrays are a special type of objects. The typeof operator in JavaScript returns "object" for arrays. But, JavaScript arrays are best described as arrays. Arrays use numbers to access its "elements". In this example, person[0] returns John:
10.3. Declaring 2D Arrays, Arrays are objects in Java, so any variable that declares an array holds a reference sets the value for the 3rd row and 2nd column of a 2D array called nums ? Passing arrays as arguments (C# Programming Guide) Arrays can be passed as arguments to method parameters. Because arrays are reference types, the method can change the value of the elements.
Comments In your case its actually just innerArray1
. But that's because your definition is weird (you are assigning inside your array definition). Your "thing" is actually a 3D array, if it is even one, because it looks more like an object in your example you can access in your "middle array" with an index, like outerArray[0] .... outerArray[N-1], but your syntax is ... weird actually Is this actually how you've defined your data? If so, you should seriously reconsider. If you're instead trying to simply describe your array structure using this format, have a look at how to create a Minimum, Complete, and Verifiable Example and edit the question with real code. This will be much more likely to produce a helpful answer to your question. Pretty much you should just get rid of the naming/assigning portion within the array(s) (middleArray1 = ...
) as it's going to just end in indexes anyway. Thanks this is exactly what I need! Yeah I was mixing up objects like you said.