Solved

Java Lists/Arrays

Posted on 2007-03-27
16
215 Views
Last Modified: 2010-03-31
How do I assign the values of a list to an array?

For example, I want to do something like:

for (int i=o; i<list.size(); i++) {
arr[i] = list.get(i);
}

The reason I want to do this is because I can't seem to be able to access the list object from another class... But I can access arrays..

Answer to either of the problems would be great!

Thanks

0
Comment
Question by:vrameen
  • 7
  • 6
  • 2
  • +1
16 Comments
 
LVL 86

Expert Comment

by:CEHJ
ID: 18801835
>>I can't seem to be able to access the list object from another class... But I can access arrays..

Then you need to put the list in the same scope as the arrays
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 18801845
If it's in the same package, make sure the List is an instance member
0
 

Author Comment

by:vrameen
ID: 18801899
@CEHJ,

Thanks for the help... I already put:

public ArrayList list = new ArrayList();

Outside of any methods etc..
Any variables I have their can be accessed, but not this one object..

Furthermore.. any ideas how I can do something like:

for (int i=o; i<list.size(); i++) {
arr[i] = list.get(i);
}

The above code just throws error about incompatible types (list is object, slots[i][j] is a string)..


0
 
LVL 10

Expert Comment

by:ADSLMark
ID: 18801903
Check your methods/attributes for private/public. It would be smart to have a get method for the list object, like:

public LinkedList getList()
{
    return this.list; //no matter if list is private or public, you access the attribute if you are in your own class
}

Another solution is to make the declaration of list public and also outside the method.
So you might have sth like:
class MyClass
{
  public void someCalculation()
  {
      LinkedList list = new LinkedList();
      .....
  }
}
but, now you cannot access list. You should have declared list outside the method like:

class MyClass
{
  private LinkedList list;
  public void someCalculation()
  {
      this.list = new LinkedList();
      .....
  }
}

while your at it, you might want to instantiate the list attribute in the constructor and add the "getter" i proposed in the beginning. The code would look sth like:

class MyClass
{
  private LinkedList list;
  public MyClass()
  {
      this.list = new LinkedList();
  }
  public void someCalculation()
  {
      .....
  }
  public LinkedList getList()
  {
      return this.list;
  }
}

Mark
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 18801957
>>
Thanks for the help... I already put:

public ArrayList list = new ArrayList();

Outside of any methods etc..
>>

That can't be the case or you'd be able to access it
>>Furthermore.. any ideas how I can do something like

'arr' should be declared as Object[] or you need to cast
0
 

Author Comment

by:vrameen
ID: 18802102
@CEHJ

Did you mean something like this?:

   public void popArr() {
       
        for(int i=0; i<listsize; i++) {
           
            mods[i] = (String) list.get(i);
        }
       
       
        for (int j=0; j<mods.length; j++) {
           
            System.out.println(mods[j]);
        }
    }

I get nullpointer exception doing the above
0
 

Author Comment

by:vrameen
ID: 18802112
Also, I really have declared the list outside of any methods..

Extract from code:

import java.sql.*;
import javax.sql.*;
import javax.swing.*;
import java.util.*;

class dbManage {
   
    final static String path = "jdbc:mysql";
    final static String host = "localhost";
    final static String port = "3306";
    final static String db = "tt";
    final static String login = "root";
    final static String user = "root";
    final static String password = "rameen";
    final static String conStr = "jdbc:mysql://localhost:3306/tt";
    public static String[] arr = new String[getModAmt()];
    private static int mod;
    public static String[] mods;
    //public static String[] arr;
    public static int listsize;
   
   public ArrayList list = new ArrayList();
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 18802172
>>mods[i] = (String) list.get(i);

You can't access element i if the List doesn't have it. Nor can you assign to mods[i] if it doesn't exist
0
How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

 

Author Comment

by:vrameen
ID: 18802258
@CEHJ

In the constructor, I've already set it to populate the list.
0
 
LVL 30

Expert Comment

by:mayankeagle
ID: 18802259
Maybe I'm missing something.

Why can't you use the toArray () method?

arr = list.toArray () ;

?
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 18802381
What is 'mods' and where is it initialized?
0
 

Author Comment

by:vrameen
ID: 18802449
in the code i posted, you can see mods is initialized the same way outside the method..

mayan, i'll give it a go!
0
 
LVL 30

Accepted Solution

by:
mayankeagle earned 500 total points
ID: 18802482
What is the data type of the array? If it is String, you can do:

arr = ( String[] ) list.toArray ( new String[0] ) ;
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 18802539
>>you can see mods is initialized the same way outside the method..

It isn't initialized at all - it's just declared

>>public static String[] mods;
0
 

Author Comment

by:vrameen
ID: 18802596
@mayankeagle

Thanks, that worked fine..

0
 

Author Comment

by:vrameen
ID: 18802600
@CEHJ

Apologies, my mistake.. I guess it wasn't initialized..
0

Featured Post

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
This video teaches viewers about errors in exception handling.

707 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now