0

I made a code using my No2 class but I'm confused about how to print out entire code without using array.

I just know one way to print out this code, I'm using System.out.println() 7 times.

Please help me to figure out another way/handy way to do it.

    public static void main(String[] args) {
         No2 a1 = new No2("1","A",3,6000);
         No2 a2 = new No2("2","B",4,8000);
         No2 a3 = new No2("3","C",1,1000);
         No2 a4 = new No2("4","D",2,4000);
         No2 a5 = new No2("5","E",5,10000);
         No2 a6 = new No2("6","F",2,4000);
         No2 a7 = new No2("7","G",3,6000);
   }
cнŝdk
  • 31,391
  • 7
  • 56
  • 78
Okem
  • 395
  • 1
  • 4
  • 12
  • 2
    Why can't you use an array? – DejaVuSansMono Nov 11 '14 at 15:25
  • I just want to find another way to print out it, but yet to find another way – Okem Nov 11 '14 at 15:32
  • 1
    Are you against any sort of collection? Can you use an ArrayList? or a List? If you can't use a collection, then you won't get much better than the above. – AdamMc331 Nov 11 '14 at 15:36
  • I just don't understand about ArrayList or List. whether we can use looping for print out code above ? – Okem Nov 11 '14 at 15:44
  • If you're willing to learn about them, beyonddc's answer is a good one. – AdamMc331 Nov 11 '14 at 15:45
  • Also, here's the [documentation](https://docs.oracle.com/javase/7/docs/api/java/util/ArrayList.html) if you are interested. Regardless of whether or not you use it here, Lists are a must-have skill in my opinion. – AdamMc331 Nov 11 '14 at 15:46
  • possible duplicate of [Java Reflection: How to get the name of a variable?](http://stackoverflow.com/questions/744226/java-reflection-how-to-get-the-name-of-a-variable) – Narmer Nov 11 '14 at 15:54

3 Answers3

2
    public static void main(String[] args) {
        final List<No2> no2List = new ArrayList<No2>();
        no2List.add(new No2("1","A",3,6000));
        no2List.add(new No2("2","B",4,8000));
        no2List.add(new No2("3","C",1,1000));
        no2List.add(new No2("4","D",2,4000));
        no2List.add(new No2("5","E",5,10000));
        no2List.add(new No2("6","F",2,4000));
        no2List.add(new No2("7","G",3,6000));

        for (final No2 no2 : no2List) {
            System.out.println(no2.toString());
        }
    }

You'll need to implement the toString() method in No2 class. Also you'll need to import java.util.List and java.util.ArrayList in your main class.

That should do the trick for you.

beyonddc
  • 1,246
  • 3
  • 13
  • 26
0

You could override the toString method in your No2 class to prettify your output. I'm totally guessing here:

@Override
public String toString(){
    return "id: " + id + ", " +  
           "letter: " + letter + ", " +
           "number: " + number+ ", " +
           "bigNumber: " + bigNumber;
}

I'm afraid that, without using a Collection, your task is nearly impossible (and quite useless). You can tough use a single println for your objects:

System.out.println(a1.toString() + "\n" +
                   a2.toString() + "\n" +
                   a3.toString() + "\n" +
                   a4.toString() + "\n" +
                   a5.toString() + "\n" +
                   a6.toString() + "\n" +
                   a7.toString() + "\n");

which is pretty ugly.

Narmer
  • 1,414
  • 7
  • 16
-1

You can implement toString method in your No2 Class, then use an array to store your variables :

array[0] = new No2("1","A",3,6000);
// ...
// Then try this line of code
Arrays.toString(array);

Good luck.

teeyo
  • 3,665
  • 3
  • 22
  • 37