Solved

Java Lists/Arrays

Posted on 2007-03-27
16
223 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Viewers learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…
The viewer will learn how to implement Singleton Design Pattern in Java.

911 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

18 Experts available now in Live!

Get 1:1 Help Now