9 Replies - 6760 Views - Last Post: 23 March 2008 - 09:05 PM Rate Topic: -----

#1 MFam  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 41
  • Joined: 03-March 08

ArrayList to Array Help

Post icon  Posted 21 March 2008 - 09:17 AM

Quote

I have an existing array with 4 elements. I am creating an add button and using the ArrayList to copy the array and what ever else I add. Then I want to return it to the original array. I am unable to modify any of my classes. I am working inside the gui area.

I think the problem is my setup or a casting issue. Can someone help? I have example and add button code.


Example:
import java.util.*;

public class Sample
{
	public static void main(String[] args)
	{
		String[] x = new String[] {"Asia", "Europe", "Africa"};
		System.out.println("Length of x ="+x.length);
		ArrayList xv = new ArrayList();
		for (int i = 0; i < x.length; i++)
			xv.add(x[i]);
		xv.add("Australia");

		System.out.println("Length of xv ="+xv.size());
		x = new String[xv.size()];
		xv.toArray(x);
		System.out.println("Length of x ="+x.length);
	}
}



MyCode:
//ADD Button 

	class AddButtonHandler implements ActionListener 
	{

		public void actionPerformed(ActionEvent event) 
	{

		
//		updateAButton( inventory);
		ArrayList<NameRestock> xv = new ArrayList<NameRestock>();
		for (i = 0; i < inventory.length;i++)
		{
			xv.add(inventory[i]);
			System.out.println("Length" + xv.size());
		}

		int iItem = Integer.parseInt(JOptionPane.showInputDialog("Enter Item Number: "));
		String Mtrl = JOptionPane.showInputDialog(null, "Enter Material Name :");
		int Amnt = Integer.parseInt(JOptionPane.showInputDialog("Enter count of product: "));
		double cost = Double.parseDouble(JOptionPane.showInputDialog("Enter cost: $ "));
		String store = JOptionPane.showInputDialog("Enter Purchase Store :");

//		xv.add(new NameRestock(5,"Glue",2,5.00,"GlueShop"));
		xv.add(new NameRestock(iItem, Mtrl, Amnt, cost, store));		 
		System.out.println("Length Now " + xv.size());
		
		
		
		System.out.println("Length of inventory = " + inventory.length);
		}
	} //end ADD Button






Is This A Good Question/Topic? 0
  • +

Replies To: ArrayList to Array Help

#2 Martyr2  Icon User is offline

  • Programming Theoretician
  • member icon

Reputation: 4334
  • View blog
  • Posts: 12,128
  • Joined: 18-April 07

Re: ArrayList to Array Help

Posted 21 March 2008 - 10:57 AM

Well if your inventory array is of type NameRestock objects you are ok there, when you go back to an array you are going to have to dump it to an array of NameRestock objects specified as a runtime type.

// Creates "newarray" of NameRestock items if "inventory" 
// array of NameRestock items is too small, otherwise it will populate
// inventory[]
NameRestock newarray[] = xv.toArray(inventory[]);



I think this is what you were talking about. If you want to dump your arraylist back to a NemeRestock array you can use toArray with the array of NameRestock type to fill otherwise it will create the other array.

Can you possibly provide the error message you are getting or the code where you are converting the arraylist back to your NameRestock array?

That is, if the above sample didn't help. :)
Was This Post Helpful? 0
  • +
  • -

#3 MFam  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 41
  • Joined: 03-March 08

Re: ArrayList to Array Help

Posted 21 March 2008 - 01:28 PM

View PostMartyr2, on 21 Mar, 2008 - 10:57 AM, said:

Well if your inventory array is of type NameRestock objects you are ok there, when you go back to an array you are going to have to dump it to an array of NameRestock objects specified as a runtime type.

// Creates "newarray" of NameRestock items if "inventory" 
// array of NameRestock items is too small, otherwise it will populate
// inventory[]
NameRestock newarray[] = xv.toArray(inventory[]);



I think this is what you were talking about. If you want to dump your arraylist back to a NemeRestock array you can use toArray with the array of NameRestock type to fill otherwise it will create the other array.

Can you possibly provide the error message you are getting or the code where you are converting the arraylist back to your NameRestock array?

That is, if the above sample didn't help. :)


Quote

I figued out how to get it to work, however, your answer does not work for me. But my answer does not completely give me what I want either. I will attache all the *.java files into a text. I have it setup to do a next and a previous. But when I add an item record using the "add" button, I can print to screen the information. I am un-able to use the next and previous button to view the added item. Can you tell me where I am going wrong with this.

Attached File(s)


Was This Post Helpful? 0
  • +
  • -

#4 MFam  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 41
  • Joined: 03-March 08

Re: ArrayList to Array Help

Posted 21 March 2008 - 04:19 PM

[quote name='MFam' date='21 Mar, 2008 - 01:28 PM' post='329632']

View PostMartyr2, on 21 Mar, 2008 - 10:57 AM, said:

Well if your inventory array is of type NameRestock objects you are ok there, when you go back to an array you are going to have to dump it to an array of NameRestock objects specified as a runtime type.

// Creates "newarray" of NameRestock items if "inventory" 
// array of NameRestock items is too small, otherwise it will populate
// inventory[]
NameRestock newarray[] = xv.toArray(inventory[]);



I think this is what you were talking about. If you want to dump your arraylist back to a NemeRestock array you can use toArray with the array of NameRestock type to fill otherwise it will create the other array.

Can you possibly provide the error message you are getting or the code where you are converting the arraylist back to your NameRestock array?

That is, if the above sample didn't help. :)


Quote

I figued out how to get it to work, however, your answer does not work for me. But my answer does not completely give me what I want either. I will attache all the *.java files into a text. I have it setup to do a next and a previous. But when I add an item record using the "add" button, I can print to screen the information. I am un-able to use the next and previous button to view the added item. Can you tell me where I am going wrong with this.

Quote

I can print out the 5 and 6th added element inside the AddButtonHandler. I am unable to access it outside this method. My next button or previous button is not picking it up. Can someone help me! I need to be able to update the array and also see it with the other buttons. What am I doing wrong! Please assist!

Was This Post Helpful? 0
  • +
  • -

#5 MFam  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 41
  • Joined: 03-March 08

Re: ArrayList to Array Help

Posted 22 March 2008 - 08:09 AM

Quote

Thanks for the help! I am having problems with my save button now. It appears to be setup properly, however, my dialogue box pops up when I click save and says "NameRestock". Can someone tell me what is setup improperly. Attached is the entire program. What I am working on is in the "gui.java" file!

Again many thanks to all those who has been patient with me and helping!

Attached File(s)


Was This Post Helpful? 0
  • +
  • -

#6 Martyr2  Icon User is offline

  • Programming Theoretician
  • member icon

Reputation: 4334
  • View blog
  • Posts: 12,128
  • Joined: 18-April 07

Re: ArrayList to Array Help

Posted 22 March 2008 - 08:48 AM

In order to serialize all objects you have must be serializable. Your product class is not. So you need to make it serializable by implementing the interface for product. NameRestock inherits from Product so it will inherit the serializable ability and thus does not need the definition.

So make the top of your product class look like this...

import java.io.*;

public class Product implements Serializable
{



Then everything should compile fine and you should see it writing out to file in a serialized manner.

Enjoy!

"At DIC we be serializing code ninjas.... and serial killers!" :snap:
Was This Post Helpful? 0
  • +
  • -

#7 MFam  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 41
  • Joined: 03-March 08

Re: ArrayList to Array Help

Posted 22 March 2008 - 09:56 AM

View PostMartyr2, on 22 Mar, 2008 - 08:48 AM, said:

In order to serialize all objects you have must be serializable. Your product class is not. So you need to make it serializable by implementing the interface for product. NameRestock inherits from Product so it will inherit the serializable ability and thus does not need the definition.

So make the top of your product class look like this...

import java.io.*;

public class Product implements Serializable
{



Then everything should compile fine and you should see it writing out to file in a serialized manner.

Enjoy!

"At DIC we be serializing code ninjas.... and serial killers!" :snap:

Quote

Unsure if previous thread were sent. I made the product.class serializable and compile. It compiled and the save button worked, however, it did not come out as text.

Was This Post Helpful? 0
  • +
  • -

#8 Martyr2  Icon User is offline

  • Programming Theoretician
  • member icon

Reputation: 4334
  • View blog
  • Posts: 12,128
  • Joined: 18-April 07

Re: ArrayList to Array Help

Posted 22 March 2008 - 10:07 AM

Of course it didn't, it is serialized data. Serialized data isn't just text, it is information about the structure of the class so that if you were to read the file back and unserialize it java could reconstruct the object (your product class etc) in the exact state it was.

If you just want to write out the contents of your inventory, you are going the totally wrong way with implementing serializable. You just need to loop through the inventory and write to the file using a BufferedWriter or something and looping through the array contents executing writeline calls.

I am only going with what you already have there. ;)
Was This Post Helpful? 0
  • +
  • -

#9 MFam  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 41
  • Joined: 03-March 08

Re: ArrayList to Array Help

Posted 23 March 2008 - 04:14 PM

Quote

I have worked out several of my problems for this week. The last couple of days I have been working on my last couple of buttons. I am having problems with the modify and delete buttons. Can someone show me how to get started.

This is due tonight, so any help will be greatly apprieciated!



I wanted to thank Marty2 for helping, I had a couple of items to setup for my save like the "inventory[ax.size] and stuff. It works great. And yes I do understand now after re-reading the material of serialization. To retrieve the data now I will basically need to un-serialize.

Attached File(s)


Was This Post Helpful? 0
  • +
  • -

#10 MFam  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 41
  • Joined: 03-March 08

Re: ArrayList to Array Help

Posted 23 March 2008 - 09:05 PM

Quote

Can someone look at my Search section and tell me what is wrong? See attached Text file.

Attached File(s)


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1