3 Replies - 5621 Views - Last Post: 13 August 2010 - 12:46 PM Rate Topic: -----

#1 Pustik   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 15
  • Joined: 12-August 10

Problem with Arraylist.set() method

Posted 13 August 2010 - 11:03 AM

Hey guys,
I need help with set method at Arraylist. I have ArrayList of class and I need to change atribute of this class in whole list. I want to use normal loop but problem is with set method. In code is just seriously easied version of real thing what I am doing but principal is same. I have no idea how to help and google didn't help:( Thanks for your help

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package procvicovanizakladu;

import java.util.ArrayList;

/**
For fun nejake zakladni operace a operandy. Tak si to vyzkousime
 */
public class Main {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {

    ArrayList<Pokus> a = new ArrayList<Pokus>();
    a.add(new Pokus(5));
    a.add(new Pokus(10));
    a.add(new Pokus(15));
    a.add(new Pokus(20)); //Adding files

    a.set(index, null);)/>   ///////Index is for sure, but how to change attribute c in class Pokus to any diffrend number?
    
    for(int i = 0 < a.size() ; i++)
    {
          System.out.println(a.get(i).c);  
    }

    }
}


And class Pokus:

package procvicovanizakladu;

/**
 *
 * @author Puštík
 */
public class Pokus {

    int c;
    public Pokus (int B)/>
    {
        c = b;
    }

}



Is This A Good Question/Topic? 0
  • +

Replies To: Problem with Arraylist.set() method

#2 macosxnerd101   User is offline

  • Games, Graphs, and Auctions
  • member icon




Reputation: 12800
  • View blog
  • Posts: 45,992
  • Joined: 27-December 08

Re: Problem with Arraylist.set() method

Posted 13 August 2010 - 12:30 PM

The set() method for ArrayList deals with the Object, not the attributes of the Object. If you want to change an Object attribute, you have to get() the Object at index, and modify the attribute from there. For example:
list.get(someIndex).c = newValue;


Was This Post Helpful? 1
  • +
  • -

#3 Pustik   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 15
  • Joined: 12-August 10

Re: Problem with Arraylist.set() method

Posted 13 August 2010 - 12:41 PM

Thank you a lot, helped to unstuck me:)
Love this site
Was This Post Helpful? 0
  • +
  • -

#4 macosxnerd101   User is offline

  • Games, Graphs, and Auctions
  • member icon




Reputation: 12800
  • View blog
  • Posts: 45,992
  • Joined: 27-December 08

Re: Problem with Arraylist.set() method

Posted 13 August 2010 - 12:46 PM

Glad I could help! :)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1