0

I would like to simple loop through all the values of the objects like i have with the values of the array. I have searched online but they seem to always complicate things much more then i can comprehend. My code shows pretty simply what i want to do.

Here is my code:

class myCON{

    int modelYear;
    String modelName;


    public myCON(int year, String name){

        modelYear = year;
        modelName = name;

    }


    public static void main(String[] args){

        /*

        ***********************************************************
        int[] arr= {1,2,3,4,5,6,7,8,9,10,11,12,13,14,15};

        for ( int i=0; i<arr.length; i++){
            System.out.println(arr[i]);
        }

        System.out.println("end");

        *************************************************************

        */


        myCON myObj1 = new myCON(2005, "Nissan5");
        myCON myObj2 = new myCON(2006, "Nissan6");
        myCON myObj3 = new myCON(2007, "Nissan7");
        myCON myObj4 = new myCON(2008, "Nissan8");
        myCON myObj5 = new myCON(2009, "Nissan9");

        for(int i=1; i<6;i++){
            System.out.println(myObj[i].modelYear + " " + myObj[i].modelName);
        }

        System.out.println(myObj1.modelYear + " " + myObj1.modelName);
        System.out.println(myObj2.modelYear + " " + myObj2.modelName);
        System.out.println(myObj3.modelYear + " " + myObj3.modelName);
        System.out.println(myObj4.modelYear + " " + myObj4.modelName);
        System.out.println(myObj5.modelYear + " " + myObj5.modelName);




    }
}
GhostCat
  • 137,827
  • 25
  • 176
  • 248
Just Aname
  • 9
  • 1
  • 2
  • What are you even iterating *through*? I don't see an `ArrayList` or similar data structure of your `myCon` object. – sleepToken Dec 30 '19 at 12:24
  • 1
    Hint: your code implies that you already know about arrays. So start by learning how to use arrays for object types. And hint: follow java naming conventions. Class names should A) be meaningful and say what they are about and B) follow the UpperCase idea. – GhostCat Dec 30 '19 at 12:35

6 Answers6

0

Store them in an array and then you can iterate over them. Like this:

myCON myObj[] = {new myCON(2005, "Nissan5"), new myCON(2006, "Nissan6")}
for(int i=0; i<myObj.length;i++){
   System.out.println(myObj[i].modelYear + " " + myObj[i].modelName);
}
Abdul Fatah
  • 463
  • 6
  • 23
0

You must add your object to list. And do looping for the process.

List<myCon> myObj = new ArrayList<>();
for(int i=0; i<5; i++) {
   myObj.add(new myCon("2005", "Nisan5");
}

And then to show it, use looping for again.

for(int i=0; i<5; i++) {
   System.out.println(myObj.get(i).modelYear + " " + myObj.get(i).modelName);
}
0

Here is what you can do:

List<myCON> myObjects = new ArrayList<>();
myCON myObj1 = new myCON(2005, "Nissan5");
myObjects.add(myObj1);
myCON myObj2 = new myCON(2006, "Nissan6");
myObjects.add(myObj2);
myCON myObj3 = new myCON(2007, "Nissan7");
myObjects.add(myObj3);

for (int i = 0; i < myObjects.size(); i++) {
    System.out.println(myObjects.get(i).modelYear + " " + 
myObjects.get(i).modelName);
}
pypie
  • 71
  • 6
  • You have heard of the for-each loop? – GhostCat Dec 30 '19 at 12:37
  • Yes! But the question is asking for a for i specifically. – pypie Dec 30 '19 at 12:40
  • No it is not. The questions asks how to iterate objects. – GhostCat Dec 30 '19 at 12:55
  • The way I interpret it is like this: they are asking for 'loop through all the values of the objects like i have with the values of the array' and they give a for i example in comments in their code, so the simplest answer is to show how it works with for i. To additionally show how it would work with for each would mean to explain two things at once, which in my opinion is unnecessarily complex (they are a new contributor, like myself). – pypie Dec 30 '19 at 12:59
0

Try this. Basically what you need to do is make a static variable list that is available to all instances and provide a static method to get the list. This way you can use it from outside the class too. Also, Class names should start with uppercase.

int modelYear;
String modelName;

/*
 * Create a static list object to store all the objects
 */
static List<myCON> objectList = new ArrayList<myCON>(); 

public myCON(int year, String name){

    modelYear = year;
    modelName = name;

    /*
     *  Add the newly created object to static list 
     */
    objectList.add(this);
}


public static void main(String[] args){
    myCON myObj1 = new myCON(2005, "Nissan5");
    myCON myObj2 = new myCON(2006, "Nissan6");
    myCON myObj3 = new myCON(2007, "Nissan7");
    myCON myObj4 = new myCON(2008, "Nissan8");
    myCON myObj5 = new myCON(2009, "Nissan9");

    for(myCON instance : getObjects()){
        System.out.println(instance.modelYear + " " + instance.modelName);
    }
}

/*
 * Create a static getter method which can be called from anywhere
 * to get the list of objects.
 */
public static List<myCON> getObjects() {
    return objectList;
}
Aman jangra
  • 268
  • 1
  • 16
  • I assumed the requirement is to get all the objects from outside the class too and not just from main method. Adding to list in the constructor itself will automatically keep track of all the objects created anytime from anywhere. I assumed adding just an array in the main method would not be a right approach as this functionality should be with the class, in my opinion. If I need the same thing in some other method, What would I do ? – Aman jangra Dec 30 '19 at 15:42
0

Haha you can't use myObj[i] to get myObj1, myObj2, ... , myObj5. If you follow the syntax of Java, myObj[] is an array and myObj[i] is the i'th element of that array.

So, you can just put those objects into the array and iterate it :)

myCon[] myObjs = new myCon[] {myObj1, myObj2, myObj3, myObj4, myObj5};

for(int i = 0; i < myObjs.length; i++) {
  System.out.println(myObjs[i].modelYear + " " + myObjs[i].modelName);
}

Also quick side note: I start with i = 0 in the for-loop instead of 1 since arrays are zero-based so myObj1 will be at myObj[0], i.e. i = 0

Hope this helps

Kerage Chan
  • 116
  • 1
  • 9
0

There is actually no array in your code.

You are just creating new instances of your class myCON.

If you want to have an Array or an ArrayList with all the instances of your myCON class you need to do the following.

class myCON {

    int modelYear;
    String modelName;

    public myCON(int year, String name) {

        modelYear = year;
        modelName = name;
    }        

    public static void main(String[] args) {

        List<myCON> myCONList = new ArrayList<>;

        myCONList.add(new myCON(2005, "Nissan5"));
        // Keep populating your List or Array until you are satisfied.
        myCONList.add(new myCON(2009, "Nissan9"));

        for (myCON m : myCONList) {
            System.out.println(m.modelYear + " " + m.modelName);
        }
    }
}

When you do :

myCON myObj1 = new myCON(2005, "Nissan5");

You don't create any array, you just create a new instance of your myCON Class and "give" it to your Object named myObj1.

Thing is myObj1 is just a name that gives you access to the content of it. The '1' at the end doesn't mean anything in particular you could have named it myObjFirst, it wouldn't have made any difference.

So when you try :

for(int i=1; i<6;i++){
    System.out.println(myObj[i].modelYear + " " + myObj[i].modelName);
}

You are trying to access myObj (not myObj1 or myObj2 or anything) which doesn't exist in your code. Adding [i] will not add the value of i at the end of your name. It will just try to see if myObj exist and is an Array so that it can access the i element of this array.

You should try to check some tutorials on Java, I think it could help you a lot.

Philippe B.
  • 485
  • 4
  • 18