/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package javaapplication1;
import java.io.*;
/**
*
* @author simon
*/
public class Main {
/**
* @param args the command line arguments
*/
public static void main(String[] args) throws IOException {
NotSimple[] objArray;
BufferedReader stdin = new BufferedReader(
new InputStreamReader( System.in ) );
System.out.println( "Enter a number of objects:" );
int size;
size = Integer.parseInt( stdin.readLine() );
//Initialize objArray
objArray = new NotSimple[size];
//TODO: Implement following functions
initializeObj(objArray);
increaseData(objArray);
printObjData(objArray);
//TODO: Explain all outputs of the below function
explainOutputs();
return;
}
//TODO
//initialize every Notsimple object in the array 'a'
//to NotSimple()
//Hint: using the for loop, assign a[i] = new NotSimple();
static void initializeObj(NotSimple[] a){
//TODO: FILL ME
for (int i = 0; i < a.length; i++)
{
a[i] = new NotSimple();
}
}
//TODO:
//Increase the ‘data’ member of every NotSimple object
//in the array ‘a’ by 1
static void increaseData(NotSimple[] a) {
//TODO: FILL ME
for (int i = 0; i < a.length; i++)
{
a[i].setData(a[i].getData()+1);
}
}
//TODO:
//Print the data of every NotSimple object in the array ‘a’
static void printObjData(NotSimple[] a) {
//TODO: FILL ME
for (int i = 0; i < a.length; i++)
{
System.out.println (a[i].getData());
}
}
//TODO explain all the outputs 1a-1f
static void explainOutputs() {
NotSimple nsObj1 = new NotSimple();
//1a
System.out.println( "nsObj1.data is\t" + nsObj1.getData() );
System.out.println( "nsObj1.str is \t" + nsObj1.getStr() );
NotSimple nsObj2 = new NotSimple( 50,
"Another immutable string!" );
//1b
System.out.println( "nsObj2.data is\t" + nsObj2.getData() );
System.out.println( "nsObj2.str is \t" + nsObj2.getStr() );
nsObj2 = nsObj1;
nsObj2.setData(10);
nsObj1.setData(100);
//1c
System.out.println( "nsObj2.data is\t" + nsObj2.getData() );
System.out.println( "nsObj2.str is \t" + nsObj2.getStr() );
nsObj1 = new NotSimple();
//1d
System.out.println( "nsObj1.data is\t" + nsObj1.getData() );
System.out.println( "nsObj1.str is \t" + nsObj1.getStr() );
System.out.println( "nsObj2.data is\t" + nsObj2.getData() );
System.out.println( "nsObj2.str is \t" + nsObj2.getStr() );
nsObj2 = new NotSimple();
//1e
System.out.println( "nsObj2.data is\t" + nsObj2.getData() );
System.out.println( "nsObj2.str is \t" + nsObj2.getStr() );
nsObj2.setData(10);
//1f
System.out.println( "nsObj1.data is\t" + nsObj1.getData() );
System.out.println( "nsObj1.str is \t" + nsObj1.getStr() );
System.out.println( "nsObj2.data is\t" + nsObj2.getData() );
System.out.println( "nsObj2.str is \t" + nsObj2.getStr() );
}
}
class NotSimple
{
NotSimple()
{
data = 5;
str = new String( "Initialized!" );
}
NotSimple( int i, String str1 )
{
data = i;
str = str1;
}
void setData( int i )
{
data = i;
return;
}
int getData()
{
return data;
}
void setStr( String str1)
{
str = str1;
return;
}
String getStr()
{
return str;
}
private int data;
private String str;
}
讲师希望我“将数组“ a”中每个NotSimple对象的“数据”成员增加1”,当我运行程序时,它只会增加第一个数据。例如,当我输入3时,我得到以下信息:
跑:
Enter a number of objects:
3
6
6
6
nsObj1.data is 5
nsObj1.str is Initialized!
nsObj2.data is 50
nsObj2.str is Another immutable string!
nsObj2.data is 100
nsObj2.str is Initialized!
nsObj1.data is 5
nsObj1.str is Initialized!
nsObj2.data is 100
nsObj2.str is Initialized!
nsObj2.data is 5
nsObj2.str is Initialized!
nsObj1.data is 5
nsObj1.str is Initialized!
nsObj2.data is 10
我的问题是,不是所有数据都加1吗?即101、6、101、6、6、11
最佳答案
您在这里有两个主要操作。首先是创建一个包含x
元素的数组,每个元素都用值5
初始化并递增一个。这给你这个结果:
3
6
6
6
3个元素(值5)递增。
代码的第二部分(explainOutputs()
)不增加任何内容。声明了两个对象(nsObj1
和nsObj2
),并在打印它们之前对其进行了手动修改。没有增量。它只是打印您的设置。
这是您所做的:NotSimple nsObj1 = new NotSimple();
打印5
。NotSimple nsObj2 = new NotSimple(50, "Another immutable string!");
版画50nsObj2 = nsObj1; nsObj2.setData(10); nsObj1.setData(100);
基本上您只是说nsObj2的值为100,它会显示100nsObj1 = new NotSimple();
nsObj1将打印5(因为它是对新对象的引用)
nsObj2仍会打印100nsObj2 = new NotSimple();
nsObj2是对新对象的引用,它将打印5nsObj2.setData(10);
nsObj1仍然打印5
nsObj2将打印10,因为仅nsObj2引用的对象的值会更改
结果:
5 50 100 5 100 5 5 10