Win a copy of Zero to AI - A non-technical, hype-free guide to prospering in the AI era this week in the Artificial Intelligence and Machine Learning forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Paul Clapham
  • Bear Bibeault
  • Jeanne Boyarsky
Sheriffs:
  • Ron McLeod
  • Tim Cooke
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Jj Roberts
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • salvin francis
  • Scott Selikoff
  • fred rosenberger

How to store in array of object?

 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello all, here I want to store the value returned by createFogDevice in array of FogDevice object, so that each time it will be stored in fd1,fd2,fd3... Like this. Intead of in one variable Fd . Please see the ** mark. Can anyone help me how to rewrite this part ?


public class Device {
static List<FogDevice> fogDevices = new ArrayList<FogDevice>();
static List<Sensor> sensors = new ArrayList<Sensor>();
static List<Actuator> actuators = new ArrayList<Actuator>();

public Device(String arr) {
    String[] a = arr.split(",");
    System.out.println(a[0]);
   String st = a[0];
    switch(st) {
    case "fog":
        System.out.println("Fog");
       **FogDevice fd = createFogDevice(arr);**
         break;
    case "sensor":
        System.out.println("Sensor");
        break;
    case "Actuator" :
        System.out.println("Actuator");
        break;
       
    }
}

    private static FogDevice createFogDevice(String b) {
   String[] a = b.split(",");
   String name = a[0];
   boolean isCloud = Boolean.parseBoolean(a[1]);
   long mips = Long.parseLong(a[2]);
   FogDevice fogdevice = "after some activity return fogdevice";
   return fogdevice ;
   
 
lowercase baba
Posts: 12908
63
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Does it HAVE to be an array? that may not be your best choice.  If so, you'll need to keep track of how many elements you've inserted already, and when you create a new element, add it in at the next empty spot, and then update your counter.

You should consider something like an arrayLIst, which handles all that for you. You simply say "add this new element".
 
fred rosenberger
lowercase baba
Posts: 12908
63
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
also, you can't dynamically create variable names, if that's what you meant by "fd1", "fd2"...  

you can create a collection object, and add elements to it, then you generally refer to a specific index of that collection..or iterate through it with a loop

 
Gravity is a harsh mistress. But this tiny ad is pretty easy to deal with:
the value of filler advertising in 2020
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic