A friendly place for programming greenhorns!
Big Moose Saloon
Search
|
Java FAQ
|
Recent Topics
Register / Login
Win a copy of
Mongo DB Applied Patterns
this week in the
MongoDB
forum
or a resume review from
Five Year Itch
in the
Jobs Discussion
forum!
JavaRanch
»
Java Forums
»
Java
»
Java in General
Author
NDimensionalBoolean
Richard Mittleman
Greenhorn
Joined: Apr 19, 2010
Posts: 10
posted
Feb 01, 2011 22:15:12
0
I have need for an N-Dimensional Boolean & wrote the following class. I would appreciate a code review of it.
Thank you
import java.util.ArrayList; public class NDimensionalBoolean { private String [] columnNames; private ArrayList<Boolean[]> values; public NDimensionalBoolean (int theNumberOfRows, String [] theColumnNames) { this.columnNames = theColumnNames; this.values = new ArrayList<Boolean[]>(theNumberOfRows); for (int row = 0; row < theNumberOfRows; row++) { this.values.add (new Boolean [columnNames.length]); setAllEntriesToTheSameValue(row, false); } } public NDimensionalBoolean() { } public boolean getValueAtRowColumn(int theRow, String theColumnName) { for (int column = 0; column < columnNames.length; column++) { if (theColumnName.equals(this.columnNames[column])) { boolean b = this.values.get(theRow) [column]; return b; } } return false; } public boolean areAllColumnsOnThisRowTrue(int theRow) { boolean results = true; for (int column = 0; column < columnNames.length; column++) { results = results && this.values.get(theRow) [column]; } return results; } public int getNumberRows() { return this.values.size(); } public int getNumberOfRowsWithAllColumnsFalse() { int numberFalse = 0; for (int row = 0; row < this.values.size(); row++) { if (! areAnyColumnsOnThisRowTrue(row)) numberFalse++; } return numberFalse; } public int getNumberOfRowsWithATrueColumn() { int numberTrue = 0; for (int row = 0; row < this.values.size(); row++) { if ( areAnyColumnsOnThisRowTrue(row)) numberTrue++; } return numberTrue; } public int getNumberTruesOnThisColumn(String theColumnName) { int numberTrue = 0; for (int row = 0; row < this.values.size(); row++) { if ( getValueAtRowColumn(row, theColumnName)) numberTrue++; } return numberTrue; } public boolean areAnyColumnsOnThisRowTrue(int theRow) { boolean results = false; for (int column = 0; column < columnNames.length; column++) { results = results || this.values.get(theRow) [column]; } return results; } public void removeRow(int theRow) { this.values.remove(theRow); return ; } public void setValueAtRowColumn(int theRow, String theColumnName, boolean theValue) { for (int column = 0; column < columnNames.length; column++) { if (theColumnName.equals(this.columnNames[column])) { this.values.get(theRow) [column] = theValue; break; } } return; } public void setAllEntriesToTheSameValue(int theRow, boolean theValue) { for (int column = 0; column < columnNames.length; column++) { this.values.get(theRow) [column] = theValue; } return; } public static void main(String[] args) { final String PARS = "PARS"; final String SKILLS = "SKILLS"; final String DUTIES = "DUTIES"; final String EDUCATIONS = "EDUCATIONS"; final String [] COLUMNNAMES = {PARS, SKILLS, DUTIES, EDUCATIONS}; int numberOfRows = 10; NDimensionalBoolean phraseUsed = new NDimensionalBoolean(numberOfRows, COLUMNNAMES); boolean value; for (int row = 0; row < numberOfRows; row+=2) { phraseUsed. setAllEntriesToTheSameValue(row, true); } for (int row = 0; row < numberOfRows; row++) { value = phraseUsed. areAllColumnsOnThisRowTrue(row); System.out.println("Row " + row + " And = " + value); value = phraseUsed. areAnyColumnsOnThisRowTrue(row); System.out.println("Row " + row + " Or = " + value); } } }
I agree. Here's the link:
http://ej-technologies/jprofiler
- if it wasn't for jprofiler, we would need to run our stuff on 16 servers instead of 3.
subject: NDimensionalBoolean
Similar Threads
Add Row with AbstractTableModel
Help with using JCheckBox in JTable to return a value in another column
jtable not displayed
TableModel question
JTable as User's input (?)
All times are in JavaRanch time: GMT-6 in summer, GMT-7 in winter