Add10 listadd20 arraylist list reference value

Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: swapArrayList (ArrayList<Integer> a) {//Since “a” is a reference value, the // changes made in “a” are visible // outside the func.on. int temp; temp=a.get(0); a.set(0,a.get(1)); a.set(1,temp); ArrayList<Integer> localV=new ArrayList<Integer>(); localV.add(100); localV.add(101); //return local arrayList is ok as a copy of the ArrayList reference is returned return localV; } public sta.c void main(String args) { ArrayList<Integer> list=new ArrayList<Integer>(); list.add(10); list.add(20); //ArrayList “list” reference value is //copied to the “a” parameter variable ArrayList<Integer> returnList=swapArrayList(list); System.out.println("list(0): "+list.get(0)+" list(1): "+list.get(1)); System.out.println("returnList(0): "+returnList.get(0)+" returnList(1): "+returnList.get(1)); }} C++ Parameter Variable is also Pass by Value – Built ­in Type Variables int swap (int a, int b) { //Since “a” and “b” are values, the // changes made on “a” and “b” are not // visible outside the func.on. int temp; temp=a; a=b; b=temp; temp=a+b...
View Full Document

This document was uploaded on 03/06/2014 for the course CS 3710 at Memorial University.

Ask a homework question - tutors are online