你能为数组中的数字添加一个数字吗?

时间:2010-10-02 04:57:08

标签: java arrays

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;
}

我想在数组中添加1,但它不起作用。我收到以下编译错误:

operator ++ cannot be applied to javaapplication1.NotSimple

3 个答案:

答案 0 :(得分:2)

使用

  for (int i = 0; i < a.length; i++)
  {
       a[i].setData(a[i].getData()+1);
  }

由于Java不支持运算符重载,因此您需要检索旧值(即getData()调用),向其中添加1,并将其设置为新值(即setData()调用)。

答案 1 :(得分:0)

NotSimple类是什么样的?
NotSimple是一个可以递增的数字类型吗?

答案 2 :(得分:0)

如果NotSimple有一个名为'data'的字段,那么将[i] ++替换为[i] .data ++