Results 1 to 5 of 5
  1. #1
    wired-in=p is offline Member
    Join Date
    Jul 2011
    Posts
    38
    Rep Power
    0

    Default Using Arrays as Variables.

    Im a beginner with Java and I need some help with a project that I am working on. As a start, I am unfamiliar with 'types'. But more importantly I need to add objects, more specifically arrays, to an arraylist. This i believe that I succeeded in as arraylists allow objects to be added as elements with basic code. I named the arraylist as megaArray. I want to be able to run a for loop starting with x at 0, adding one to the integer each time it loops around. Using this for loop, I want to be able to search the index of every array in megaArray to see if it has a certain object as an element, which i put as a parameter. The code that I am setting up to accomplish this is not working. The code, or thought process is as follows;

    public void someMethod (object c)
    for (int x = 0; x < 15...number of arrays in the arraylist...; x++) {
    megaArray[x].findIndex (object c)

    //each object is n element in 3 of the 15 arrays within megaArray
    //after it returns an integer >= 0, if the array contains the object, or another integer, of which I don't know, if the array does not contain the object
    //how do i get the computer to acknowledge those three arrays? is there any way i can give those three arrays a variable name?


    thank you very much for your time, i know that this may be incredibly basic but im new to programming, and especially OOP.

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,421
    Rep Power
    25

    Default

    Java Code:
    megaArray[x].findIndex (object c)
    If megaArray is an ArrayList this is the wrong syntax. What you have coded is for megaArray as an array. The [] are for array references.
    To call a method of a class you use the . to connect the method to the object reference/pointer; here it is megaArray.
    megaArray.findIndex(..)
    Read the API doc for the findIndex() method. It does not look inside of an an object in the arraylist
    To find an object in an array, you need to get a reference to that array and search that arrray.
    is there any way i can give those three arrays a variable name
    You would need three variable names, one each array.
    You can define three variables as references to arrays and then assign them the values of the references to the arrays that are contained in the arraylist.
    <datatype>[] oneDimAr1 = megaArray.get(ix); // get a reference to one of the arrays in the arraylist
    <datatype>[] oneDimAr2 = megaArray.get(iy); // get a reference to another of the arrays in the arraylist

  3. #3
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default

    I want to be able to search the index of every array in megaArray to see if it has a certain object as an element
    Do you mean that you want to test whether some object is an element of every array in the list? If so you just loop through the list elements (the arrays) and check.

    Java Code:
    import java.util.ArrayList;
    import java.util.List;
    
    public class MegaArray {
    
    	public static void main(String[] args) {
    		List<int[]> megaArray = new ArrayList<int[]>();
    		
    			// put 15 int arrays into the list
    			// so that it contains [[0,0,0],[0,1,2],[0,2,4],[0,3,6],etc]
    		for(int ndx = 0; ndx < 15; ndx++) {
    			megaArray.add(new int[] {ndx * 0, ndx * 1, ndx * 2});
    		}
    		
    			// test to see if some value is common
    		System.out.println("10 is in all list arrays: " + find(megaArray, 10));
    		System.out.println("0 is in all list arrays: " + find(megaArray, 0));
    	}
    	
    	private static boolean find(List<int[]> list, int target) {
    		for(int ndx = 0; ndx < list.size(); ndx++) {
    			int[] arr = list.get(ndx);
    			boolean found = false;
    			for(int arrNdx = 0; arrNdx < arr.length; arrNdx++) {
    				if(arr[arrNdx] == target) {
    					found = true;
    				}
    			}
    			if(!found) { // the target is not in this array
    				return false;
    			}
    		}
    		return true;
    	}
    }
    (There is nothing special about this example using ints as the elements of the arrays.)

    I don't know what the three arrays are that you want the computer to "acknowledge". So perhaps you mean to do something else.

  4. #4
    wired-in=p is offline Member
    Join Date
    Jul 2011
    Posts
    38
    Rep Power
    0

    Default

    Thank you both, this was very helpful. But pbrockway2 is megaArray a list or an arraylist? I am not familiar with the syntax of creating a new instance.

  5. #5
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,421
    Rep Power
    25

    Default

    List<int[]> megaArray = new ArrayList<int[]>();
    If you read the API doc you will see that ArrayList implements the List interface.
    You need to read about this technique of using interfaces to give a data type to a family of classes to allow any of that family's objects to be used as parameters to a utility class like you find in Collections.
    Look at the List interface API doc and see all the different classes that implement it. Any method that takes a List object as an argument can take one of those classes.

Similar Threads

  1. arrays and multidimensional arrays
    By belfast09 in forum New To Java
    Replies: 5
    Last Post: 06-14-2011, 01:28 PM
  2. help with variables please
    By arimaliz in forum New To Java
    Replies: 3
    Last Post: 03-22-2011, 04:30 PM
  3. Arrays.sort... why sorting all arrays in class?
    By innspiron in forum New To Java
    Replies: 6
    Last Post: 03-23-2010, 01:40 AM
  4. What are Instance variables and static variables?
    By sandeshforu in forum New To Java
    Replies: 3
    Last Post: 09-09-2009, 05:48 PM
  5. Variables
    By mew in forum New To Java
    Replies: 3
    Last Post: 12-11-2007, 12:44 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •