• 周一. 12月 11th, 2023

5G编程聚合网

5G时代下一个聚合的编程学习网

热门标签

Storing in an array from my own created class – Java From 5gw.org

[db:作者]

4月 10, 2022

xmlns=”http://www.w3.org/1999/xhtml” dir=”ltr” lang=”en”>
Storing in an array from my own created class – Java
/**
* Bytes CSS
* Style: ‘bytes.answers’; Style ID: 18
*/
@import url(“/clientscript/vbulletin_css/style-12635a82-00018.css”);

 
 
470,370 Members | 1,585 Online

Storing in an array from my own created class

Hi,
Here I am again with more questions. I am groveling for help.
I am working on a program in Netbeans 5.5.1 where I created a class called Student that gets and sets Last Name, First Name and Student ID. Now the program to run as a samle of this class will store the first Student I get and put it into slot [0] of my array and then I increment my NumStudents, as a count to moveon to the next array slot. The problem is that it only puts the Student info into slot[0], even on the 2nd or 3rd entry to store information. I’m not sure if the problem is in the gets and sets in my class or in my Store Info button listener.
Here is a bit of the code. I have comments to help explain my problem.

Thank you oh virtuosos of Java! This stuff brings me to tears!

Expand|Select|Wrap|Line Numbers
  1.   private void mouseclickedbtnStoreInfo(java.awt.event.MouseEvent evt) 
  2. {                                          
  3. // Code to store info from the textbox entered by user:
  4.  
  5.  
  6.         s.setFirstName(txtFirstName.getText()); 
  7.  
  8.         s.setLastName(txtLastName.getText());
  9.  
  10.         s.setStudentID(txtStudentID.getText());
  11.  
  12.         arrayAllStudents[NumStu] = s;
  13.         NumStu++;
  14.  
  15.         txtFirstName.setText("");
  16.         txtLastName.setText("");
  17.         txtStudentID.setText("");
  18.  
  19.         //Testing array storage here also 
  20.  
  21. /*This only prints out only for the second student entered, loses the first, NumStu times, apparently only storing in slot 0. array is of Student class as is the variable s.*/
  22.  
  23.     for(int i=0;i<NumStu;i++){
  24.             System.out.println(arrayAllStudents[i].getStudentID());
  25.             System.out.println(arrayAllStudents[i].getFirstName());
  26.             System.out.println(arrayAllStudents[i].getLastName());    
  27.       }
  28.  
  29.     }   

Hopefully this is not a challenging puzzle for all you experts out there. Pop me a line soon. I’ll take my turn helping when I get this #*@& figured out.

Oct 19 ’07
#1

3 1255


(adsbygoogle = window.adsbygoogle || []).push({});

JosAH

11,448
Expert 8TB

Create a new student every time you want to store a student in that array. As I
can see from your code you’re storing the same student ‘s’ in every slot of your
array; you’re just changing it’s name etc. over and over again so all slots end
up pointing to the same student ‘s’ which has the last changed name etc. in it.

kind regards,

Jos

Oct 20 ’07
#2

Thank you so much. I don’t quite understand that if the first Student is in my array already why it doesn’t stay as the same information when I call for the next Student. Although, I do sort of get that the Student class is only holding the one Student object that I set. Anyway, I did set a new student at every action of my Store Info button and now it works great, just like you said.

I appreciate the help. I know these basics must be a bore for you to see as questions and confusions with us beginners.
Again, Thank you!

Oct 21 ’07
#3

JosAH

11,448
Expert 8TB

Thank you so much. I don’t quite understand that if the first Student is in my array already why it doesn’t stay as the same information when I call for the next Student. Although, I do sort of get that the Student class is only holding the one Student object that I set. Anyway, I did set a new student at every action of my Store Info button and now it works great, just like you said.

I appreciate the help. I know these basics must be a bore for you to see as questions and confusions with us beginners.
Again, Thank you!

You’re welcome of course; let’s play a strange little game: suppose you’re an
array with two elements; represented by your left and right hand. Also suppose
I’m a student ‘s’.

First your code gives me the name "Fred" and makes your first element slot
point to me; so now your left hand points to me and my name is Fred. Next,
your code changes my name to "Barney".

Your left hand points to me, but my name is "Barney" now. Your code makes
your second slot point to me as well. Now both your hands point to one and
the same student: me. But my name is Barney now. So both your hands point
to the same student ‘s;, named Barney.

If you instead had created a new student object when you wanted to fill a next
slot of the array and named *that* one Barney or whatever, your left and right
hand would’ve pointed to two different students, both with their own name.

kind regards,

Jos

Oct 21 ’07
#4


Message
 
Cancel Changes

Post your reply

Sign in to post your reply or Sign up for a free account.

By using this site, you agree to our Privacy Policy and Terms of Use.


(function(i,s,o,g,r,a,m){i[‘GoogleAnalyticsObject’]=r;i[r]=i[r]||function(){
(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
})(window,document,’script’,’//www.google-analytics.com/analytics.js’,’ga’);

ga(‘create’, ‘UA-90111-7’, ‘bytes.com’);
ga(‘send’, ‘pageview’);

loadBytesTracking()

//0){location.replace(‘https://bytes.com/showthread.php?p=’+cpostno);};} }

if(typeof window.orig_onload == “function”) window.orig_onload();
}

//]]>

《Storing in an array from my own created class – Java From 5gw.org》有30个想法

发表回复

您的电子邮箱地址不会被公开。 必填项已用*标注