Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Java Lists/Arrays

Posted on 2007-03-27
16
Medium Priority
?
253 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
Build and deliver software with DevOps

A digital transformation requires faster time to market, shorter software development lifecycles, and the ability to adapt rapidly to changing customer demands. DevOps provides the solution.

 
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 2000 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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
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 third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
Suggested Courses

722 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