13 Replies - 602 Views - Last Post: 09 April 2012 - 12:51 PM Rate Topic: -----

#1 Twister1002  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 47
  • Joined: 08-April 12

Using multi-deminsion arrays question (or theory)

Posted 08 April 2012 - 05:09 PM

Hey you! I just joined the forums and hoping to get into the zone with Java! As my first post, I have a question about a certain issue I'm facing. (What new comer doesn't? haha).

Little Background: Now, I have been a web developer (Not a professional, but some projects for fun) and I understand things about Java and C#. I have had class in Java and C# so it's not too unfamiliar. Plus I love to code. THIS IS NOT FORIEN TO ME!

I HAVE A THEORY!

Objective: Understanding Multi-dimensional arrays, using correctly, and learn what methods I need to take for this specific action.

Issue: I am doing a query in a class and I am wanting to return values in an ArrayList and letting another class access the information and using it. Plus I need to use any kind of Data Type (Major ones like: String, Int, Double, etc). Now, my only question I have, is HOW do I use them?

For instance (Idk who is familiar with PHP) however, in PHP, you can set KEYS in the array
<?php 
$customer = array(){
"firstName" => "Tyler",
"lastName" => "burger",
};
?>



So I was needing something similar in Java. However I can not find anything, nor any way to really use arrays effectively without making tons of repetitive stuff. I understand how to use ArrayList<(DataType)>, but here are some things I do not understand.
ArrayList<ArrayList<String>> customers = new ArrayList<ArrayList<String>>();

ArrayList<String[][]> customers = new ArrayList<String[][]>();




Some of you might be thinking, "Ahh will just use normal arrays...". The problem stands, I am getting data from a database which will be adding things constantly to the database, so there is no true way to set an array before adding the data to the array. (I can not make the ResultSet go to backwards because I'm using SQLite.)

ANY help or information is GREATLY appreciated. Thank you for the time for reading this. =)

Is This A Good Question/Topic? 0
  • +

Replies To: Using multi-deminsion arrays question (or theory)

#2 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8343
  • View blog
  • Posts: 31,890
  • Joined: 06-March 08

Re: Using multi-deminsion arrays question (or theory)

Posted 08 April 2012 - 05:58 PM

Usually in Java you create an ArrayList of an object representing your selec in an SQL
loop on the ResultSet
create an Object out of that ResulSet results
add it to the ArrayList

Pseudo code
ResultSet rs = con.select * from table
ArrayList<Person> al = new ArrayList<Person>();
while(rs.next()) {
    String lastName = rs.nextString(1);
    String firstName = rs.nextString(2);
    al.add(new Person(lastName, firstName);
}


Was This Post Helpful? 0
  • +
  • -

#3 Twister1002  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 47
  • Joined: 08-April 12

Re: Using multi-deminsion arrays question (or theory)

Posted 09 April 2012 - 06:11 AM

Hummm... I really dont have a way to make a new object like that though.

All classes I have right now are:
Classes: 

DatabaseConnection - Create a connection and close it
Select - All select statements (Inculdes workers, customers, and other data)
Delete - Removes them from the active list (Inculdes workers, customers, and other data)
Insert - Adds data to the database (Inculdes workers, customers, and other data)
Update - Updates data (Inculdes workers, customers, and other data)
Results - Grabs the information for what ever query was sent.



Which I mean it could be organized more, I do believe. Also I need to be able to access that data at any time as well.
Was This Post Helpful? 0
  • +
  • -

#4 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8343
  • View blog
  • Posts: 31,890
  • Joined: 06-March 08

Re: Using multi-deminsion arrays question (or theory)

Posted 09 April 2012 - 07:12 AM

View PostTwister1002, on 09 April 2012 - 09:11 AM, said:

Hummm... I really dont have a way to make a new object like that though.

Why ? As long as you select one column from a database you can make an object out of it

Quote

Also I need to be able to access that data at any time as well.

Yes the ArrayList can be make permanently available
Was This Post Helpful? 0
  • +
  • -

#5 Twister1002  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 47
  • Joined: 08-April 12

Re: Using multi-deminsion arrays question (or theory)

Posted 09 April 2012 - 07:30 AM

View Postpbl, on 09 April 2012 - 07:12 AM, said:

Why ? As long as you select one column from a database you can make an object out of it


Confused? How can you create an object that is not real?
Was This Post Helpful? 0
  • +
  • -

#6 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8343
  • View blog
  • Posts: 31,890
  • Joined: 06-March 08

Re: Using multi-deminsion arrays question (or theory)

Posted 09 April 2012 - 07:34 AM

What do you think is the difference between a real object and a non real object ?
Never heard of the concept of real/no-real object
Was This Post Helpful? 0
  • +
  • -

#7 Twister1002  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 47
  • Joined: 08-April 12

Re: Using multi-deminsion arrays question (or theory)

Posted 09 April 2012 - 07:41 AM

The only thing I've ever been taught to create a new object is by creating a class to make that object. The real and non-real object is me just not understanding how you can create an object by a table inside of a database by fetching information from a column.
Was This Post Helpful? 0
  • +
  • -

#8 baavgai  Icon User is online

  • Dreaming Coder
  • member icon

Reputation: 5906
  • View blog
  • Posts: 12,811
  • Joined: 16-October 07

Re: Using multi-deminsion arrays question (or theory)

Posted 09 April 2012 - 07:45 AM

View PostTwister1002, on 09 April 2012 - 08:11 AM, said:

Hummm... I really dont have a way to make a new object like that though.


?!? If it's your code, you can always make more objects!

Looks like standard CRUD. e.g.
interface DataItem {
	void delete();
	void update();
	boolean hasChanges();
}

interface Worker extends DataItem { }

interface Customer extends DataItem { }

interface DataManager  {
	List<Customer> selectAllCustomers();
	List<Worker> selectAllWorkers();
	Customer selectCustomer(int id);
	Worker selectWorker(int id);
	Customer createCustomer(int id);
	Worker createWorker(int id);
}


Was This Post Helpful? 0
  • +
  • -

#9 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8343
  • View blog
  • Posts: 31,890
  • Joined: 06-March 08

Re: Using multi-deminsion arrays question (or theory)

Posted 09 April 2012 - 07:47 AM

// this class represents one row in the database
// the first column is a String, the second column an int and the third column is a Date
class TableRow {
   String col1;
   int col2;
   java.sql.Date col3;

   TableRow(String name, int age, java.sql.Date birthday) {
       col1 = name;
       col2 = age;
       col3 = bithday;
   }
}


Was This Post Helpful? 0
  • +
  • -

#10 Twister1002  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 47
  • Joined: 08-April 12

Re: Using multi-deminsion arrays question (or theory)

Posted 09 April 2012 - 08:26 AM

View Postbaavgai, on 09 April 2012 - 07:45 AM, said:

?!? If it's your code, you can always make more objects!

Looks like standard CRUD. e.g.
interface DataItem {
	void delete();
	void update();
	boolean hasChanges();
}

interface Worker extends DataItem { }

interface Customer extends DataItem { }

interface DataManager  {
	List<Customer> selectAllCustomers();
	List<Worker> selectAllWorkers();
	Customer selectCustomer(int id);
	Worker selectWorker(int id);
	Customer createCustomer(int id);
	Worker createWorker(int id);
}



I know it is my code, but I'm not advanced in Java, as I want to be. I'm still heavily learning it. I do remember interfaces, but I'm going to have to go read them again, and see how to use them.

View Postpbl, on 09 April 2012 - 07:47 AM, said:

// this class represents one row in the database
// the first column is a String, the second column an int and the third column is a Date
class TableRow {
   String col1;
   int col2;
   java.sql.Date col3;

   TableRow(String name, int age, java.sql.Date birthday) {
       col1 = name;
       col2 = age;
       col3 = birthday;
   }
}



Not to sure where you were going on this...
Was This Post Helpful? 0
  • +
  • -

#11 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8343
  • View blog
  • Posts: 31,890
  • Joined: 06-March 08

Re: Using multi-deminsion arrays question (or theory)

Posted 09 April 2012 - 08:36 AM

View PostTwister1002, on 09 April 2012 - 11:26 AM, said:

// this class represents one row in the database
// the first column is a String, the second column an int and the third column is a Date
class TableRow {
   String col1;
   int col2;
   java.sql.Date col3;



Not to sure where you were going on this...

Trying to reply to your question

Quote

you can create an object by a table inside of a database by fetching information from a column.

Was This Post Helpful? 0
  • +
  • -

#12 baavgai  Icon User is online

  • Dreaming Coder
  • member icon

Reputation: 5906
  • View blog
  • Posts: 12,811
  • Joined: 16-October 07

Re: Using multi-deminsion arrays question (or theory)

Posted 09 April 2012 - 09:07 AM

Ok, let's step back. You offered:
<?php
$customer = array(){
"firstName" => "Tyler",
"lastName" => "burger",
};
?>



In very basic Java, you could do:
class Customer {
	public String firstName;
	public String lastName;
}

Customer customer = new Customer();
customer.firstName = "Tyler";
customer.lastName = "burger";



However, you're looking at:
ArrayList<String[][]> customers = new ArrayList<String[][]>();



Baby steps... how about:
class DataObject {
	public String name;
	// this is your list of field values
	public Map<String, String> fields;
	public DataObject(String name) { 
		this.name = name;
		this.fields = new HashMap<String,String>()
	}
}

List<DataObject> customers = new ArrayList<DataObject>();

DataObject customer = new DataObject("Customer");
customer.fields.put("firstName", "Tyler");
customer.fields.put("lastName", "burger");
customers.add(customer);



It's still basically duct typing, but better than a 2D string array. Honestly, anything is better than a multidimensional array. If you're even unwilling to give the object a name, at least use a map.

Beyond that, I'd strongly recommend getting more familiar with the language you're working with. Good luck.
Was This Post Helpful? 2
  • +
  • -

#13 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10781
  • View blog
  • Posts: 40,153
  • Joined: 27-December 08

Re: Using multi-deminsion arrays question (or theory)

Posted 09 April 2012 - 11:54 AM

My tutorial Moving Away From Parallel Arrays covers more on class design in Java. If you are using a relational database model, stick with using JDBC and interacting with relational databases. When in Java though, stay in the Java OOP world.

Also, I want to add some commentary on the PHP side of things. PHP's mysql_*() family of functions is very ugly and practically deprecated (they will be formally deprecated in the next version of PHP). These functions cause many users to ignore the fact that PHP supports classes and objects as well. In fact, PDO allows you to better integrate your own classes with database queries.
Was This Post Helpful? 0
  • +
  • -

#14 Twister1002  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 47
  • Joined: 08-April 12

Re: Using multi-deminsion arrays question (or theory)

Posted 09 April 2012 - 12:51 PM

View Postpbl, on 09 April 2012 - 08:36 AM, said:

View PostTwister1002, on 09 April 2012 - 11:26 AM, said:

// this class represents one row in the database
// the first column is a String, the second column an int and the third column is a Date
class TableRow {
   String col1;
   int col2;
   java.sql.Date col3;



Not to sure where you were going on this...

Trying to reply to your question

Quote

you can create an object by a table inside of a database by fetching information from a column.


The only thing I truly understand is that you are showing my different datatypes that can be used. I understand datatypes. I do appreciate your help though.

View Postbaavgai, on 09 April 2012 - 09:07 AM, said:

Ok, let's step back. You offered:
<?php
$customer = array(){
"firstName" => "Tyler",
"lastName" => "burger",
};
?>



In very basic Java, you could do:
class Customer {
	public String firstName;
	public String lastName;
}

Customer customer = new Customer();
customer.firstName = "Tyler";
customer.lastName = "burger";



However, you're looking at:
ArrayList<String[][]> customers = new ArrayList<String[][]>();



Baby steps... how about:
class DataObject {
	public String name;
	// this is your list of field values
	public Map<String, String> fields;
	public DataObject(String name) { 
		this.name = name;
		this.fields = new HashMap<String,String>()
	}
}

List<DataObject> customers = new ArrayList<DataObject>();

DataObject customer = new DataObject("Customer");
customer.fields.put("firstName", "Tyler");
customer.fields.put("lastName", "burger");
customers.add(customer);



It's still basically duct typing, but better than a 2D string array. Honestly, anything is better than a multidimensional array. If you're even unwilling to give the object a name, at least use a map.

Beyond that, I'd strongly recommend getting more familiar with the language you're working with. Good luck.


Honestly this has probably helped me the most about what I am wanting to do. I understand that arrays are probably out of my league for what I want to do. After I started reading again on interfaces, I feel like that is going to be the best route to do this project with. So I will have to start playing around with interfaces. Also
Map<String, ArrayList> personInfo = new HashMap<String, ArrayList>(); 

will probably be my route if I decide to use it. I very much appreciate your help as well.

View Postmacosxnerd101, on 09 April 2012 - 11:54 AM, said:

My tutorial Moving Away From Parallel Arrays covers more on class design in Java. If you are using a relational database model, stick with using JDBC and interacting with relational databases. When in Java though, stay in the Java OOP world.

Also, I want to add some commentary on the PHP side of things. PHP's mysql_*() family of functions is very ugly and practically deprecated (they will be formally deprecated in the next version of PHP). These functions cause many users to ignore the fact that PHP supports classes and objects as well. In fact, PDO allows you to better integrate your own classes with database queries.


Sorry if I may have been confusing. I was just showing an example from PHP for a correlation into a Java thing I was trying to achieve. I am not using PHP for this program, it is purely Java. I started used PHP OOP stuff, but I havent really gotten in depth of it. It's been a pain for me recently. Thank you for the links though. I read through your article, and I found it very nice. So now I will need to be using an interface, and classes.

I really appreciate everyone's time for helping me out on this topic.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1