Solved

Java Lists/Arrays

Posted on 2007-03-27
16
231 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
Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

 
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
 

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:Mayank S
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:
Mayank S 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

ScreenConnect 6.0 Free Trial

Want empowering updates? You're in the right place! Discover new features in ScreenConnect 6.0, based on partner feedback, to keep you business operating smoothly and optimally (the way it should be). Explore all of the extras and enhancements for yourself!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
login jsp example 24 50
eclipse apache tomcat admin console 52 97
tomcat administrtor 12 47
throw exception 21 44
For customizing the look of your lightweight component and making it look opaque like it was made of plastic.  This tip assumes your component to be of rectangular shape and completely opaque.   (CODE)
Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:

810 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