Can I use public void from a class to make use of it in ArrayList to add age and name?
I'm new to Java and I'm trying to get my head around constructs, classes and objects. I apologise if any of this seems stupid to you.
I have been tasked with creating an ArrayList to hold items of type Data (my class). using the .add method - I should add a certain number of names and ages then output.
I have sort of done it, but I was wondering if I could use the functions like SetAge and SetAge from my class to use within my ArrayList to create a new person and then output it together like I have with the rest. Is there another way?
Thank you for your help & explanation.
Below is my code;
import java.util.ArrayList;
public class workingOn
public static void main (String args)
Data Fred = new Data("Fred", 21);
Data Jo = new Data("Jo", 43);
Data Zoe = new Data("Zoe", 37);
ArrayList<Data> myArray = new ArrayList<Data>();
myArray.add(Fred);
myArray.add(Jo);
myArray.add(Zoe);
for (Data temp : myArray)
System.out.println(temp.toString());
Below is my class;
public class Data
private String name;
private int age;
Data(String n,int a)
name = n;
age = a;
public String GetName()
return(name);
public void SetName(String n)
name = n;
public int GetAge()
return(age);
public void SetAge(int a)
age = a;
public void Print()
System.out.print(("("+ GetName() ));
System.out.print(",");
System.out.print(GetAge());
System.out.print(") ");
//i made this so I don't output the object id
public String toString()
return (name + ", " + age);
java class object arraylist constructor
|
show 7 more comments
I'm new to Java and I'm trying to get my head around constructs, classes and objects. I apologise if any of this seems stupid to you.
I have been tasked with creating an ArrayList to hold items of type Data (my class). using the .add method - I should add a certain number of names and ages then output.
I have sort of done it, but I was wondering if I could use the functions like SetAge and SetAge from my class to use within my ArrayList to create a new person and then output it together like I have with the rest. Is there another way?
Thank you for your help & explanation.
Below is my code;
import java.util.ArrayList;
public class workingOn
public static void main (String args)
Data Fred = new Data("Fred", 21);
Data Jo = new Data("Jo", 43);
Data Zoe = new Data("Zoe", 37);
ArrayList<Data> myArray = new ArrayList<Data>();
myArray.add(Fred);
myArray.add(Jo);
myArray.add(Zoe);
for (Data temp : myArray)
System.out.println(temp.toString());
Below is my class;
public class Data
private String name;
private int age;
Data(String n,int a)
name = n;
age = a;
public String GetName()
return(name);
public void SetName(String n)
name = n;
public int GetAge()
return(age);
public void SetAge(int a)
age = a;
public void Print()
System.out.print(("("+ GetName() ));
System.out.print(",");
System.out.print(GetAge());
System.out.print(") ");
//i made this so I don't output the object id
public String toString()
return (name + ", " + age);
java class object arraylist constructor
Just to be clear - you want to accept external input (e.g. from a keyboard) and add multiple users to this list, right?
– Makoto
Nov 14 '18 at 21:37
@Makoto I'm not looking to accept external input. I'm simply making up random users to a list that I'm making. Thank you for the question.
– dencn
Nov 14 '18 at 21:39
what do you mean byIs there another way?is it not working?
– Deadpool
Nov 14 '18 at 21:40
Okay - I'm not entirely sure what's wrong with what you've got, then. You have three users which exist in this list. Unless you're getting some kind of stack trace...I'm not convinced there's an issue...?
– Makoto
Nov 14 '18 at 21:42
@Deadpool The program is working, however I was wondering if and how I can make use of the Data class functions like SetName and SetAge within my ArrayList to create a 4th person. Would it be possible?
– dencn
Nov 14 '18 at 21:43
|
show 7 more comments
I'm new to Java and I'm trying to get my head around constructs, classes and objects. I apologise if any of this seems stupid to you.
I have been tasked with creating an ArrayList to hold items of type Data (my class). using the .add method - I should add a certain number of names and ages then output.
I have sort of done it, but I was wondering if I could use the functions like SetAge and SetAge from my class to use within my ArrayList to create a new person and then output it together like I have with the rest. Is there another way?
Thank you for your help & explanation.
Below is my code;
import java.util.ArrayList;
public class workingOn
public static void main (String args)
Data Fred = new Data("Fred", 21);
Data Jo = new Data("Jo", 43);
Data Zoe = new Data("Zoe", 37);
ArrayList<Data> myArray = new ArrayList<Data>();
myArray.add(Fred);
myArray.add(Jo);
myArray.add(Zoe);
for (Data temp : myArray)
System.out.println(temp.toString());
Below is my class;
public class Data
private String name;
private int age;
Data(String n,int a)
name = n;
age = a;
public String GetName()
return(name);
public void SetName(String n)
name = n;
public int GetAge()
return(age);
public void SetAge(int a)
age = a;
public void Print()
System.out.print(("("+ GetName() ));
System.out.print(",");
System.out.print(GetAge());
System.out.print(") ");
//i made this so I don't output the object id
public String toString()
return (name + ", " + age);
java class object arraylist constructor
I'm new to Java and I'm trying to get my head around constructs, classes and objects. I apologise if any of this seems stupid to you.
I have been tasked with creating an ArrayList to hold items of type Data (my class). using the .add method - I should add a certain number of names and ages then output.
I have sort of done it, but I was wondering if I could use the functions like SetAge and SetAge from my class to use within my ArrayList to create a new person and then output it together like I have with the rest. Is there another way?
Thank you for your help & explanation.
Below is my code;
import java.util.ArrayList;
public class workingOn
public static void main (String args)
Data Fred = new Data("Fred", 21);
Data Jo = new Data("Jo", 43);
Data Zoe = new Data("Zoe", 37);
ArrayList<Data> myArray = new ArrayList<Data>();
myArray.add(Fred);
myArray.add(Jo);
myArray.add(Zoe);
for (Data temp : myArray)
System.out.println(temp.toString());
Below is my class;
public class Data
private String name;
private int age;
Data(String n,int a)
name = n;
age = a;
public String GetName()
return(name);
public void SetName(String n)
name = n;
public int GetAge()
return(age);
public void SetAge(int a)
age = a;
public void Print()
System.out.print(("("+ GetName() ));
System.out.print(",");
System.out.print(GetAge());
System.out.print(") ");
//i made this so I don't output the object id
public String toString()
return (name + ", " + age);
java class object arraylist constructor
java class object arraylist constructor
asked Nov 14 '18 at 21:35
dencndencn
12
12
Just to be clear - you want to accept external input (e.g. from a keyboard) and add multiple users to this list, right?
– Makoto
Nov 14 '18 at 21:37
@Makoto I'm not looking to accept external input. I'm simply making up random users to a list that I'm making. Thank you for the question.
– dencn
Nov 14 '18 at 21:39
what do you mean byIs there another way?is it not working?
– Deadpool
Nov 14 '18 at 21:40
Okay - I'm not entirely sure what's wrong with what you've got, then. You have three users which exist in this list. Unless you're getting some kind of stack trace...I'm not convinced there's an issue...?
– Makoto
Nov 14 '18 at 21:42
@Deadpool The program is working, however I was wondering if and how I can make use of the Data class functions like SetName and SetAge within my ArrayList to create a 4th person. Would it be possible?
– dencn
Nov 14 '18 at 21:43
|
show 7 more comments
Just to be clear - you want to accept external input (e.g. from a keyboard) and add multiple users to this list, right?
– Makoto
Nov 14 '18 at 21:37
@Makoto I'm not looking to accept external input. I'm simply making up random users to a list that I'm making. Thank you for the question.
– dencn
Nov 14 '18 at 21:39
what do you mean byIs there another way?is it not working?
– Deadpool
Nov 14 '18 at 21:40
Okay - I'm not entirely sure what's wrong with what you've got, then. You have three users which exist in this list. Unless you're getting some kind of stack trace...I'm not convinced there's an issue...?
– Makoto
Nov 14 '18 at 21:42
@Deadpool The program is working, however I was wondering if and how I can make use of the Data class functions like SetName and SetAge within my ArrayList to create a 4th person. Would it be possible?
– dencn
Nov 14 '18 at 21:43
Just to be clear - you want to accept external input (e.g. from a keyboard) and add multiple users to this list, right?
– Makoto
Nov 14 '18 at 21:37
Just to be clear - you want to accept external input (e.g. from a keyboard) and add multiple users to this list, right?
– Makoto
Nov 14 '18 at 21:37
@Makoto I'm not looking to accept external input. I'm simply making up random users to a list that I'm making. Thank you for the question.
– dencn
Nov 14 '18 at 21:39
@Makoto I'm not looking to accept external input. I'm simply making up random users to a list that I'm making. Thank you for the question.
– dencn
Nov 14 '18 at 21:39
what do you mean by
Is there another way? is it not working?– Deadpool
Nov 14 '18 at 21:40
what do you mean by
Is there another way? is it not working?– Deadpool
Nov 14 '18 at 21:40
Okay - I'm not entirely sure what's wrong with what you've got, then. You have three users which exist in this list. Unless you're getting some kind of stack trace...I'm not convinced there's an issue...?
– Makoto
Nov 14 '18 at 21:42
Okay - I'm not entirely sure what's wrong with what you've got, then. You have three users which exist in this list. Unless you're getting some kind of stack trace...I'm not convinced there's an issue...?
– Makoto
Nov 14 '18 at 21:42
@Deadpool The program is working, however I was wondering if and how I can make use of the Data class functions like SetName and SetAge within my ArrayList to create a 4th person. Would it be possible?
– dencn
Nov 14 '18 at 21:43
@Deadpool The program is working, however I was wondering if and how I can make use of the Data class functions like SetName and SetAge within my ArrayList to create a 4th person. Would it be possible?
– dencn
Nov 14 '18 at 21:43
|
show 7 more comments
1 Answer
1
active
oldest
votes
Going through the comments, I am guessing you want to somehow use the setter methods (setName() and setAge()) for the class that you have designed. The easiest way would be:
Data d = new Data("tempName", 10);
myArray.add(d);
This basically creates an object of type Data and adds it to the arraylist. Let's assume that you want to change the name and age of this person you just added, then you should do this:
myArray.get(myArray.size() - 1).setName("newName");
myArray.get(myArray.size() - 1).setAge(18);
To add some explanation, when you add an object to an arraylist, it always adds in the end. We use size() method to get the number of objects in the Arraylist. Since index starts from 0, we use myArray.size() - 1. We use the .get() method to retrieve the object from the arraylist which takes a number as a parameter and return the object at that index.
Combining these two, we get the object at the last possible index, and call the setName() and setAge() function on it.
Hope this helps.
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53309076%2fcan-i-use-public-void-from-a-class-to-make-use-of-it-in-arraylist-to-add-age-and%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
Going through the comments, I am guessing you want to somehow use the setter methods (setName() and setAge()) for the class that you have designed. The easiest way would be:
Data d = new Data("tempName", 10);
myArray.add(d);
This basically creates an object of type Data and adds it to the arraylist. Let's assume that you want to change the name and age of this person you just added, then you should do this:
myArray.get(myArray.size() - 1).setName("newName");
myArray.get(myArray.size() - 1).setAge(18);
To add some explanation, when you add an object to an arraylist, it always adds in the end. We use size() method to get the number of objects in the Arraylist. Since index starts from 0, we use myArray.size() - 1. We use the .get() method to retrieve the object from the arraylist which takes a number as a parameter and return the object at that index.
Combining these two, we get the object at the last possible index, and call the setName() and setAge() function on it.
Hope this helps.
add a comment |
Going through the comments, I am guessing you want to somehow use the setter methods (setName() and setAge()) for the class that you have designed. The easiest way would be:
Data d = new Data("tempName", 10);
myArray.add(d);
This basically creates an object of type Data and adds it to the arraylist. Let's assume that you want to change the name and age of this person you just added, then you should do this:
myArray.get(myArray.size() - 1).setName("newName");
myArray.get(myArray.size() - 1).setAge(18);
To add some explanation, when you add an object to an arraylist, it always adds in the end. We use size() method to get the number of objects in the Arraylist. Since index starts from 0, we use myArray.size() - 1. We use the .get() method to retrieve the object from the arraylist which takes a number as a parameter and return the object at that index.
Combining these two, we get the object at the last possible index, and call the setName() and setAge() function on it.
Hope this helps.
add a comment |
Going through the comments, I am guessing you want to somehow use the setter methods (setName() and setAge()) for the class that you have designed. The easiest way would be:
Data d = new Data("tempName", 10);
myArray.add(d);
This basically creates an object of type Data and adds it to the arraylist. Let's assume that you want to change the name and age of this person you just added, then you should do this:
myArray.get(myArray.size() - 1).setName("newName");
myArray.get(myArray.size() - 1).setAge(18);
To add some explanation, when you add an object to an arraylist, it always adds in the end. We use size() method to get the number of objects in the Arraylist. Since index starts from 0, we use myArray.size() - 1. We use the .get() method to retrieve the object from the arraylist which takes a number as a parameter and return the object at that index.
Combining these two, we get the object at the last possible index, and call the setName() and setAge() function on it.
Hope this helps.
Going through the comments, I am guessing you want to somehow use the setter methods (setName() and setAge()) for the class that you have designed. The easiest way would be:
Data d = new Data("tempName", 10);
myArray.add(d);
This basically creates an object of type Data and adds it to the arraylist. Let's assume that you want to change the name and age of this person you just added, then you should do this:
myArray.get(myArray.size() - 1).setName("newName");
myArray.get(myArray.size() - 1).setAge(18);
To add some explanation, when you add an object to an arraylist, it always adds in the end. We use size() method to get the number of objects in the Arraylist. Since index starts from 0, we use myArray.size() - 1. We use the .get() method to retrieve the object from the arraylist which takes a number as a parameter and return the object at that index.
Combining these two, we get the object at the last possible index, and call the setName() and setAge() function on it.
Hope this helps.
answered Nov 15 '18 at 23:47
Arun SubramanianArun Subramanian
1038
1038
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53309076%2fcan-i-use-public-void-from-a-class-to-make-use-of-it-in-arraylist-to-add-age-and%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Just to be clear - you want to accept external input (e.g. from a keyboard) and add multiple users to this list, right?
– Makoto
Nov 14 '18 at 21:37
@Makoto I'm not looking to accept external input. I'm simply making up random users to a list that I'm making. Thank you for the question.
– dencn
Nov 14 '18 at 21:39
what do you mean by
Is there another way?is it not working?– Deadpool
Nov 14 '18 at 21:40
Okay - I'm not entirely sure what's wrong with what you've got, then. You have three users which exist in this list. Unless you're getting some kind of stack trace...I'm not convinced there's an issue...?
– Makoto
Nov 14 '18 at 21:42
@Deadpool The program is working, however I was wondering if and how I can make use of the Data class functions like SetName and SetAge within my ArrayList to create a 4th person. Would it be possible?
– dencn
Nov 14 '18 at 21:43