• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Bear Bibeault
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Paul Clapham
  • Devaka Cooray
  • Knute Snortum
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Frits Walraven
Bartenders:
  • Carey Brown
  • salvin francis
  • Claude Moore

Deep Copy ArrayList - newbie  RSS feed

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, can you please help complete the following:

1) write accessor for “transactions” field.
“transactions” is stored as an ArrayList field of long integers.

Is the below correct?


import java.util.ArrayList;
 
 public class CustomerData extends Data {
         private ArrayList<Long> transactions;
(...)
public ArrayList<Long> getTransactions() {
   ArrayList<Long>  transactions = null;
   ArrayList_new<Long> = transactions;
   return ArrayList_new;

   
 
Carey Brown
Bartender
Posts: 5771
56
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
 
Jonny Balcon
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Jonny Balcon wrote:Hi, can you please help complete the following:

1) write accessor for “transactions” field.
“transactions” is stored as an ArrayList field of long integers.

Is the below correct?



   

 
Jonny Balcon
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello Carey, there are errors. It does not compile.

It is a textbook exercise and it is  requirement I should execute a deep copy.

Carey Brown wrote:Please UseCodeTags.

First question: does it compile?

 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Jonny,

Next question: what do these errors tell you? The first errors you will encounter are:

Line 8: Look at line 8 in your example. These compilation errors are thrown because line 8 isn't valid Java code. Try to find out which statements to use, and how to order/use them correctly.
Line 9: After that line 9 will become a problem, you are returning an object type instead of an actual object.

Try to fix these two lines first. That should fix the compilation errors, and you should be able to continu the exercise.

If you still encounter any problems, we're here to help you out.

Marc.
 
Ranch Hand
Posts: 358
2
Fedora Netbeans IDE Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The layout seems right.  Where in the world did you get arraylist_new from ??
 
Sheriff
Posts: 5917
155
Chrome Eclipse IDE Java Postgres Database Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Let's look at the code you wrote carefully:
You have the method signature correct for an accessor in line 6.  In line 7 you create a local variable transactions that hides the member variable on line 4.  This is probably not what you want to do.  Line 8 uses a type ArrayList_new.  There is no such type in the standard library.  Did you create it?  Then in line 9 you return the type, not the value.  This is a syntax error.

Most accessors are three lines long, so you are complicating things.  I don't know what a "deep copy" has to do with this code.
 
Carey Brown
Bartender
Posts: 5771
56
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Jonny Balcon wrote:It is a textbook exercise and it is  requirement I should execute a deep copy.


Can you tell us the source of the exercise and exactly what the requirements are?
 
Carey Brown
Bartender
Posts: 5771
56
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You might find the List#addAll() method to be useful. You can use it to make a new list but the contents of the new list will refer to the same objects of type "Long". Having the same reference to a Long is not a problem because a Long is immutable and cannot be modified.
 
I found a beautiful pie. And a tiny ad:
Create Edit Print & Convert PDF Using Free API with Java
https://coderanch.com/wiki/703735/Create-Convert-PDF-Free-Spire
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!