Home > front end >  add array into array
add array into array

Time:09-23

I have data of tracks & tracklinks like folowing:

trackname - abc
links - www.abc.com
        www.abc1.com
        www.abc2.com

trackname - xyz
links - www.xyz.com
        www.xyz1.com
        www.xyz2.com

I want to make array with in array in Java. so final array would be:

trackdata = {    
[0] {
   [trackname] = 'abc',
   [tracklinks] = {
    [0] = "www.abc.com";
    [1] = "www.abc1.com";
    [2] = "www.abc2.com";
   }
},
[1] {
   [trackname] = 'xyz',
   [tracklinks] = {
    [0] = "www.xyz.com";
    [1] = "www.xyz1.com";
    [2] = "www.xyz2.com";
   }
}

I have tried to make this using ArrayList, Map but not succeed.

Map<String,String> map = new HashMap<>();
        
        map.put("trackname", "abc");

ArrayList<String> myLinks= new ArrayList<>();
        
        myLinks.add("www.abc.com");
        myLinks.add("www.abc1.com");
        myLinks.add("www.abc2.com");

map.put("tracklinks", myLinks);

please help me here.

CodePudding user response:

You should create a class and then access the properties like you want.

class TrackData {
        private String trackme;
        private List<String> trackLink;
        
        public String getTrackme() {return trackme;}
        public void setTrackme(String trackme) {this.trackme = trackme;}
        public List<String> getTrackLink() {return trackLink;}
        public void setTrackLink(List<String> trackLink) {this.trackLink = trackLink;}
    }

To access it:

@Test
    void arrayInArray_Test1() {
        List<TrackData> trackData = new ArrayList<>();
        trackData.add(new TrackData(){{
            setTrackme("abc");
            setTrackLink(new ArrayList<String>(){{
                add("www.abc.com");
                add("www.abc1.com");
                add("www.abc2.com");
            }});
        }});
        trackData.add(new TrackData(){{
            setTrackme("xyz");
            setTrackLink(new ArrayList<String>(){{
                add("www.xyz.com");
                add("www.xyz1.com");
                add("www.xyz2.com");
            }});
        }});
        System.out.println(trackData);
    }

If you are using a newer Java version, you can create a record instead of a class.

CodePudding user response:

You can achieve as follows

public class TrackTest {
    public static void main(String[] args) {
        List<Tracks> trackList = new ArrayList<>();
        
        Tracks track1 = new Tracks("abc");
        track1.getTrackLinks().add("www.abc.com");
        track1.getTrackLinks().add("www.abc1.com");
        track1.getTrackLinks().add("www.abc2.com");
        
        
        Tracks track2 = new Tracks("xyz");
        track2.getTrackLinks().add("www.xyz.com");
        track2.getTrackLinks().add("www.xyz1.com");
        track2.getTrackLinks().add("www.xyz2.com");
    
        trackList.add(track1);
        trackList.add(track2);
        
        System.out.println(trackList);
    }
        
    static class Tracks{
        private String trackName;
        private List<String> trackLinks;
        
        public Tracks(String trackName) {
            this.trackName = trackName;
            this.trackLinks = new ArrayList<>();
        }
        
        public Tracks(String trackName, List<String> trackLinks) {
            this.trackName = trackName;
            this.trackLinks = trackLinks;
        }

        public String getTrackName() {
            return trackName;
        }

        public List<String> getTrackLinks() {
            return trackLinks;
        }

        @Override
        public String toString() {
            return "Tracks [trackName="   trackName   ", trackLinks="   trackLinks   "]";
        }
    }
}

Let me know, if you want other approach.

CodePudding user response:

how are u?

Why u dont do this.

Create class named URL, for example.

    public class Url(){
//atributes
String domain;
String url;



//Contructor
public class URL(String domain, String url){
this.domain = domain;
this.url = url;
}
}

In ur main.class, u can create one Arraylist to saves ur instaces of URL.

public static void newURL(){
String domain, url;
Scanner keyboard = new Scanner(System.in);
//ask domain, i will use an example.
System.out.println("What is domain of URL?");
domain = keyboard.nextLine();
System.out.println("What is url?");
url = keyboard.nextLine;

//now, u have atributes of new url
URL url = new URL(domain,url);

}

What its ur objective? It's important question. If is for simple control in a little program, u can do this

CodePudding user response:

Consider using a multimap, a map whose values are list objects:

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

public class Starter {

    public static void main(String[] args) {
        
        Map<String, ArrayList<String>> map = new HashMap<String, ArrayList<String>>();
                        
        ArrayList<String> myLinks= new ArrayList<>();
                
                myLinks.add("www.abc.com");
                myLinks.add("www.abc1.com");
                myLinks.add("www.abc2.com");
        
        map.put("abc", myLinks);
        
        System.out.println(map); // {abc=[www.abc.com, www.abc1.com, www.abc2.com]}

    }

}
  • Related