Solved

Java Lists/Arrays

Posted on 2007-03-27
16
241 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
This video teaches viewers about errors in exception handling.

740 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