5 Replies - 3771 Views - Last Post: 08 October 2012 - 12:36 PM Rate Topic: -----

#1 celebi  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 24-September 12

How to use a switch statement with arrayLists?

Posted 06 October 2012 - 08:10 AM

I'm new to java and this is my first time working with an arrayList. I want to do something like this:

if (myArray[0] == 2) {
do something
}

else if (myArray[1] == 4) {
do something else
}
etc. I have 5 different if statements I need to use, but I'd like to use a switch statement instead. I can't seem to write anything correctly because Eclipse keeps giving me errors about using an array instead of an arrayList. Can someone show me how to use this kind of switch statement with my arrayList? Is it even possible or do I have to convert it into a normal array before I can work with it?

Is This A Good Question/Topic? 0
  • +

Replies To: How to use a switch statement with arrayLists?

#2 GregBrannon  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2198
  • View blog
  • Posts: 5,226
  • Joined: 10-September 10

Re: How to use a switch statement with arrayLists?

Posted 06 October 2012 - 08:17 AM

I assume you have to use an ArrayList, which is fine, but you'll need to adjust your thinking to accommodate them.

You might review the ArrayList API and the available methods for a solution.

This post has been edited by GregBrannon: 06 October 2012 - 08:18 AM

Was This Post Helpful? 0
  • +
  • -

#3 Larry71  Icon User is offline

  • New D.I.C Head

Reputation: 3
  • View blog
  • Posts: 43
  • Joined: 05-June 12

Re: How to use a switch statement with arrayLists?

Posted 06 October 2012 - 08:57 AM

A quick solution could be to use the method ArrayList.toArray().
It may fit in your case.
Was This Post Helpful? 0
  • +
  • -

#4 Ytry  Icon User is offline

  • D.I.C Head

Reputation: 16
  • View blog
  • Posts: 120
  • Joined: 25-July 12

Re: How to use a switch statement with arrayLists?

Posted 06 October 2012 - 09:06 AM

Also just a tip use code tags in front of your code, it make it easier to read. In case you don't know how just write [code ] your code here [/code ] if you remove the space following the word code, it will apply syntax highlighting to your code.
Was This Post Helpful? 0
  • +
  • -

#5 CasiOo  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1401
  • View blog
  • Posts: 3,109
  • Joined: 05-April 11

Re: How to use a switch statement with arrayLists?

Posted 06 October 2012 - 09:26 AM

ArrayList doesn't have indexer like in c#, so you cant write
myArray[1]



You will have to use the get(int) method of ArrayList to get the element at the index :)
ArrayList<Integer> myArray = new ArrayList<Integer>();

int size = myArray.size();
for (int i=0; i<size; i++) {
	int element = myArray.get(i);
	
	switch (element) {
		case 2:
			//code goes here
			break;
		case 4:
			//code goes here
			break;
	}
}


Was This Post Helpful? 1
  • +
  • -

#6 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8334
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: How to use a switch statement with arrayLists?

Posted 08 October 2012 - 12:36 PM

This
   if (myArray[0] == 2) {
       do something
   } else if (myArray[1] == 4) {
       do something else
}


ss not a candidate for a switch()
A switch, switches on a single value here you
if myArray[0]
else if mayArray[1]

you could have used a switch() if you had

if myArray[0]
else if mayArray[0]
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1