java


Find how many of one element is in a 2D array


I am having trouble attempting to find how many integers other than 0 exist in a 2D array. How do I figure this out?
My attempted code:
for (int i = 0; i < game.length; i++) {
for (int j = 0; j < game[i].length; j++) {
if (game[i][j] != 0) {
landOwned++;
}
}
Sadly that did not work, and my logic was horrible there -- just to say -- it was a guess. I tried to what things were not zero. For some reason, this didn't work correctly.
Here is my array (game) code: static int[][] game = new int[10][10];
A simple 10x10 2D int array.
I believe I found the answer:
Since my game automatically generates water in the array (int number 4 (stored into the array)) I had to add an extra if statement inside the game[i][j] != 0 statement. I totally forgot all about that, but thanks for your help.

Related Links

Why won't my code compile?
BasicDataSource connection leak
Why would this simple code throw “NoSuchElementException”?
Java(JavaFX): Maven Proguard doesnt obfuscate FX Ids
Detect & remove a range of colors from Java BufferedImage
Executing .exe file from java
Invoking the default constructor (in Java) in a class that only has a defined constructor that requires parameters
Unwanted external RPM requirements when packaging a play 2.5 app with sbt-native-packager
Java PreparedStatement Comment on Table
jersey REST layer for existing project
Kryonet connection ID limit?
Scope of a variable ends in a while loop [duplicate]
How do you display # of information between 2 number from a Array?
Android DialogFragment NullpointerException
Java contains() method of ArrayList does not return value
Java Class Casting Exceptions Generics

Categories

HOME
openlayers
testng
listview
keycloak
image-processing
drivers
google-play
plone
plot
lodash
electron
cplex
programming-languages
filtering
malloc
electronics
jpeg
networkx
indesign
cloudkit
row
quicklook
resize
zebra-printers
msp430
django-simple-history
pugjs
paging
cloudhub
xpages-ssjs
qhull
semantic-analysis
traffic
uninstall
publish
neo4j-spatial
buck
libssl
primitive
librato
microsoft-chart-controls
contact-form
perlin-noise
environment-modules
swisscomdev
hot-module-replacement
code-search-engine
c11
temporary-files
user-controls
glew
wptoolkit
long-polling
hibernate-tools
upstart
jquery-validate
logparser
paxos
togetherjs
csound
sqlbulkcopy
dynamics-sl
gnome-shell-extensions
google-cdn
messenger
jquery-filter
infix-notation
bstr
whois
ipconfig
ios4
energy
javax.sound.midi
intellij-14
django-unittest
fouc
wyam
embedded-code
generic-programming
skos
streambase
inmobi
dia
gui-test-framework
uitouch
braille
datagridviewcolumn
algebraic-data-types
prettify
oam
ms-project-server-2010
tidy
sitemesh
file-locking
jplaton
eventlistener
free-variable
typoscript2
amazon-appstore
spring-portlet-mvc
assembly-loading
zpt
cinema-4d
fluent-interface
subviews
suppress
gin
nhibernate.search
firefox-5
substrings
w3wp
dbisam
multiple-languages

Resources

Mobile Apps Dev
Database Users
javascript
java
csharp
php
android
MS Developer
developer works
python
ios
c
html
jquery
RDBMS discuss
Cloud Virtualization
Database Dev&Adm
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App